提交 084ab449 编写于 作者: 小刘28's avatar 小刘28 💬

feat:添加vuex;

上级 7b3c65c1
......@@ -3,8 +3,13 @@ import App from './App'
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
import store from './store/index';
Vue.prototype.$store = store;
App.mpType = 'app'
const app = new Vue({
store,
...App
})
app.$mount()
......
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title" @click="choose()">{{title}}</text>
</view>
<view class="">
<button
type="default"
@click="clickGetUser()"
>获取用户信息</button>
<button
type="default"
@click="clickUpdateUser()"
>更新用户信息</button>
</view>
</template>
......@@ -11,52 +15,27 @@
export default {
data() {
return {
title: 'Hello'
}
},
onShow() {
},
methods: {
choose(){
uni.chooseFile({
count: 10,
type: 'image',
success (res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFiles
console.log(tempFilePaths);
}
})
clickGetUser(){
console.log(this.$store.getters.getUser);
},
clickUpdateUser(){
let obj = {
name:'张三',
sex:''
};
this.$store.commit('updateUser',obj);
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
import Vue from 'vue';
import Vuex from 'vuex';
//vue的插件机制
Vue.use(Vuex);
//Vuex.Store 构造器选项
const store = new Vuex.Store({
state: { //存放状态
user:uni.getStorageSync('user')||{},
token:uni.getStorageSync('token')||''
},
mutations:{
M_updateUser(state,payload){
let obj = state.user;
state.user = {...obj,...payload};
uni.setStorageSync('user',state.user);
},
M_updateToken(state,payload){
state.token = payload;
uni.setStorageSync('token',state.token);
}
},
actions:{
A_updateUser(context,payload){
context.commit('M_updateUser',payload);
},
A_updateToken(context,payload){
context.commit('M_updateToken',payload);
}
},
getters:{
getUser(state){
return state.user;
},
getToken(state){
return state.token;
}
},
modules:{
}
});
export default store;
......@@ -16,10 +16,10 @@ export default {
deepClone(obj) {
//判断拷贝的obj是对象还是数组
let objClone = Array.isArray(obj) ? [] : {};
if (obj && typeof obj === "object") { //obj不能为空,并且是对象或者是数组 因为null也是object
if (obj && typeof obj === 'object') { //obj不能为空,并且是对象或者是数组 因为null也是object
for (key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] && typeof obj[key] === "object") { //obj里面属性值不为空并且还是对象,进行深度拷贝
if (obj[key] && typeof obj[key] === 'object') { //obj里面属性值不为空并且还是对象,进行深度拷贝
objClone[key] = deepClone(obj[key]); //递归进行深度的拷贝
} else {
objClone[key] = obj[key]; //直接拷贝
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册