store/login.js 商店代码
import {
defineStore
} from 'pinia';
import Cache from '@/utils/cache';
import {
reactive, toRefs
} from 'vue';
export const useCounterStore = defineStore('loginPinia', {
state: () => {
return {
isLogin: false,
token: Cache.get('TOKEN') || null
};
},
actions: {
LOGIN(opt){
this.token =opt.token
Cache.set('TOKEN', opt.token);
Cache.set('USER_INFO', opt);
}
},
});
js 端读取
import {
useCounterStore
} from '@/store/login';
const loginPinia = new useCounterStore();
main.js 代码
import App from './App'
import * as Pinia from 'pinia';
// 引入全局uview-plus
import uviewPlus from './uni_modules/uview-plus/index.js'
// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
app.use(Pinia.createPinia());
app.use(uviewPlus)
return {
app,
Pinia
}
}
// #endif
2***@qq.com
解决了吗?
2023-07-28 16:35
1***@qq.com
回复 2***@qq.com: 解决了,就是useCounterStore()这个函数调用不能放在setup外面,简单来说就是调用太早了,注意就行
2023-09-08 15:33