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

TypeScript 函式定義字串型別相連和JavaScript String.prototype.concat() 的差異

TypeScript 函式定義字串型別相連和JavaScript String.prototype.concat() 的差異

Day 150

Day 150

1 專案作品 - 使用Vite建立React專案

1 專案作品 - 使用Vite建立React專案


Comments