最近Vue3新開專案都會附上資料夾stores內的counter.ts之範例,其實有其它實用的寫法:
example.ts
import { defineStore } from 'pinia'
export const AccessControlList = defineStore('accessControlList', {
state: () => ({
/**
* 暫存存取權限List
*/
storeAccessControlList: [] as IRoleFunction[]
}),
actions: {
/**
* 設定全域存取權限List
* @param value
*/
setGlobalAccessControlList(value: IRoleFunction[]) {
this.storeAccessControlList = value
},
/**
* 移除全域存取權限List
*/
removeGlobalAccessControlList() {
this.storeAccessControlList = []
}
}
})
在其它頁面引用:
import { AccessControlList } from '@/stores/example'
const store = AccessControlList()
store.setGlobalAccessControlList(dataList)
store.removeGlobalAccessControlList()
export default defineComponent({
computed: {
...mapState(AccessControlList, ['storeAccessControlList'])
},
})