例子1
在vue-router 的 beforeEach 中實(shí)現(xiàn)如下異步邏輯:
beforeEach 開(kāi)始 -> pinia init -> beforeEach 完成
pinia 中方法定義
export const mainStore = defineStore('mainPinia', () => {
let hasInit = ref(false);
let postTypes = ref(null) as any;
let departments = ref(null) as any;
const initPinia = async () => {
console.log("init in main pinia")
if(hasInit.value){
return;
}
const res = await axios.all([
postTypesListApi(),
optionsApi()]
)
postTypes.value = res[0];
departments.value = res[1];
hasInit.value = true;
}
return {
postTypes,
departments,
initPinia
}
}
vue-router 的方法實(shí)現(xiàn)
store.dispatch('profiles/getUserInfo')
.then(async (r:any) => {
const res = await mainStoreApp.initPinia();
})