Vue3使用pinia之用法


Posted by hotmaneil on 2024-07-30

最近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'])
  },
})

#vue3 #pinia







Related Posts

如何做響應式排版

如何做響應式排版

自學經驗分享

自學經驗分享

賦值後畫面卻沒更新?你聽過Vue.set()嗎

賦值後畫面卻沒更新?你聽過Vue.set()嗎


Comments