存token
import { defineStore } from 'pinia';
export const userInfo = defineStore('userInfo', {
state: () => {
return {
userToken: uni.getStorageSync('token') || '',
};
},
actions: {
// 添加token
updateToken(token: string) {
uni.setStorageSync('token', token);
this.userToken = token
}
}
});
使用
import {
userInfo
} from '@/stores/userInfo'
import {
ref
} from "vue";
const test = userInfo()
const tst = ref('4')
test.updateToken(tst.value)
效果
这边存 用户信息
定义store 文件
import { defineStore } from 'pinia'
interface Member {
userInfo : AnyObject | null
}
const useMemberStore = defineStore('member', {
state: () : Member => {
return {
userInfo: uni.getStorageSync('userInfo') || null
}
},
actions: {
updateUserInfo(userInfo : AnyObject) {
uni.setStorageSync('userInfo', userInfo)
this.userInfo = userInfo
}
}
})
export default useMemberStore
vue页面 存储数据
import useMemberStore from '@/stores/member'
const user = useMemberStore()
user.updateUserInfo('存对象的数据')
取数据
在template中 使用 userInfo.member_id
import { storeToRefs } from 'pinia'
const info = useMemberStore()
const { userInfo } = storeToRefs(info)
console.log(userInfo.value.member_id, '从缓存中取数据');
打印下 userInfo