一.定义
/src/pinia下创建一个主文件index.ts
import { createPinia } from "pinia"
export const store = createPinia()
在mian.ts中挂载主文件
import { createSSRApp } from "vue"
import App from "./App.vue"
import { store } from "./store"
export const createApp = function () {
const app = createSSRApp(App).use(store)
return {
app
}
}
在/src/pinia下创建需要创建的store,比如创建一个user.ts
import { defineStore } from "pinia"
interface UserStoreState{
sysInfo : AnyObject, //获取手机部分信息
}
export const useUserStore = defineStore("userStore", { //全局的缓存
state: () : UserStoreState=> ({
sysInfo: {},
}),
actions: {
setSysInfo(sysInfo : AnyObject) {
this.sysInfo = sysInfo
},
},
})
二.引用
import { useUserStore } from "@/store/user"
const userStore = useUserStore()
userStore.setSysInfo('你的数据');
//取数据直接读取就好
console.log(userStore.sysInfo)
tips:
1.如果想在页面上响应pinia的变化,需要
let payLoading = ref(computed(() => {
return globalStore.payLoading
}));
这样才能相应到,直接在页面写{{globalStore.payLoading}}是无法响应的