pinia修改数据后,页面没有渲染,值是修改成功的
store.js
页面效果
【相关推荐】
名字自己定我的是pinia-store,在目录下新建index.js和login.js,代码如下
index.js
import { createPinia } from 'pinia'
const store = createPinia()
export default store
login.js
import { defineStore } from 'pinia'
export const useloginStore = defineStore({
id: 'login', // id必填,且需要唯一
state: () => {
return {
userName: uni.getStorageSync('userName') ? uni.getStorageSync('userName') : '未登录'
}
},
// actions 用来修改 state
actions: {
login(userName) {
uni.setStorageSync('userName', userName)
this.userName = userName
},
logout() {
uni.clearStorage()
this.userName = '已退出登录'
}
}
})
对比vuex的store/index.js
注意:如果只使用pinia这段代码可以没有
import { createStore } from "vuex"
const store = createStore({
state: {
"userName": uni.getStorageSync('userName') ? uni.getStorageSync('userName') : '未登录'
},
mutations: {
MLOGIN(state, userName) {
uni.setStorageSync('userName', userName)
state.userName = userName
},
MLOGOUT(state) {
uni.clearStorage()
state.userName = '已退出登录'
}
},
actions: {
login(context, userName) {
context.commit('MLOGIN', userName)
},
logout(context) {
context.commit('MLOGOUT')
}
}
})
export default store