1.新建一個(gè)文件夾store
2.新建文件[name].ts
3.定義倉(cāng)庫(kù)Store
import { defineStore } from 'pinia'
4.我們需要知道存儲(chǔ)是使用定義的defineStore(),并且它需要一個(gè)唯一的名稱,作為第一個(gè)參數(shù)傳遞
我這兒名稱抽離出去了
新建文件store-namespace/index.ts
export const enum Names {
Test = 'TEST'
}
store 引入
import { defineStore } from 'pinia'
import { Names } from './store-namespace'
export const useTestStore = defineStore(Names.Test, {
})
這個(gè)名稱,也稱為id,是必要的,Pania 使用它來(lái)將商店連接到 devtools。將返回的函數(shù)命名為use...是可組合項(xiàng)之間的約定,以使其使用習(xí)慣。
5.定義值
state 箭頭函數(shù) 返回一個(gè)對(duì)象 在對(duì)象里面定義值
import { defineStore } from 'pinia'
import { Names } from './store-namespce'
export const useTestStore = defineStore(Names.Test, {
state:()=>{
return {
current:1
}
}
})
import { defineStore } from 'pinia'
import { Names } from './store-namespce'
export const useTestStore = defineStore(Names.Test, {
state:()=>{
return {
current:1
}
},
//類似于computed 可以幫我們?nèi)バ揎椢覀兊闹? getters:{
},
//可以操作異步 和 同步提交state
actions:{
}
})