學(xué)習(xí)Pinia 第二章(初始化倉(cāng)庫(kù)Store)

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:{
 
     }
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容