欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文掌握Pinia使用及數(shù)據(jù)持久化存儲(chǔ)超詳細(xì)教程

 更新時(shí)間:2023年07月18日 11:46:32   作者:G_ing  
這篇文章主要介紹了Pinia安裝使用及數(shù)據(jù)持久化存儲(chǔ)的超詳細(xì)教程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一.安裝及使用Pinia

1.安裝Pinia兩種方式都可,根據(jù)個(gè)人習(xí)慣來(lái)

npm install pinia
yarn add pinia

2.在main.ts 中引入并掛載到根實(shí)例

// src/main.ts
import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from 'pinia'
// 創(chuàng)建Vue應(yīng)用實(shí)例
// 實(shí)例化 Pinia
// 以插件形式掛載Pinia實(shí)例
createApp(App).use(createPinia()).mount('#app')

3.src目錄下新建store/study/index.js并寫(xiě)入

Store 是用defineStore()定義的,它的第一個(gè)參數(shù)是一個(gè)獨(dú)一無(wú)二的id,也是必須傳入的,Pinia 將用它來(lái)連接 store 和 devtools。

defineStore()第二個(gè)參數(shù)可接受兩類值:Setup函數(shù)或Options對(duì)象

state 屬性: 用來(lái)存儲(chǔ)全局的狀態(tài)的,這里邊定義的,就可以是為SPA里全局的狀態(tài)了。

getters屬性:用來(lái)監(jiān)視或者說(shuō)是計(jì)算狀態(tài)的變化的,有緩存的功能。

actions屬性:對(duì)state里數(shù)據(jù)變化的業(yè)務(wù)邏輯,需求不同,編寫(xiě)邏輯不同。說(shuō)白了就是修改state全局狀態(tài)數(shù)據(jù)的。

第一種Options式寫(xiě)法:

import { defineStore } from 'pinia'
//  `defineStore()` 的返回值命名最好使用 store 的名字,同時(shí)以 `use` 開(kāi)頭且以 `Store` 結(jié)尾
export const useStudyStore = defineStore('studyId', {
  state: () => {
    return {
      counter: 0,
    }
  },
  getters:{},
  actions:{}
})

在Options式中:Store 的數(shù)據(jù)(data),getters 是 Store 的計(jì)算屬性(computed),而actions則是 Store 的方法(methods)。

第二種Setup式寫(xiě)法:

import { defineStore } from 'pinia'
//  `defineStore()` 的返回值命名最好使用 store 的名字,同時(shí)以 `use` 開(kāi)頭且以 `Store` 結(jié)尾
export const useStudyStore = defineStore('studyId', ()=>{
  const count = ref(0)
  const name = ref('Ghmin')
  const computedTest= computed(() => count.value * 99)
  function int() {
    count.value++
  }
  return { count, name, computedTest, int}
})

在Setup式中:ref()成為state屬性,computed()變成getters,function變成actions

4.使用Store

使用上面兩種方式其中一種后,便可以在組件中使用Store了。

<script setup>
import { useStudyStore } from '@/stores/study'
const store = useStudyStore();
</script>

二.具體使用及屬性與方法

1.定義數(shù)據(jù)

import { defineStore } from 'pinia'
export const useStudyStore = defineStore('studyId', {
  state: () => {
    return {
      name: 'Ghmin',
      num:0
    }
  },
})

2.組件中使用

<template>
	<div>
		<h1>vue組件</h1>
		{{ name }}
	</div>
</template>
<script setup>
import { useStudyStore } from '@/stores/study'
const store = useStudyStore();
let { name } = store;
</script>

注:pinia可以直接修改state數(shù)據(jù),無(wú)需像vuex一樣通過(guò)mutations才可以修改,所以上面的let { name } = store 這種解構(gòu)是不推薦的,因?yàn)樗茐牧隧憫?yīng)性。

而為了從 Store 中提取屬性,同時(shí)保持其響應(yīng)性,這里需要使用storeToRefs(),它將為每個(gè)響應(yīng)性屬性創(chuàng)建引用。當(dāng)你只使用 Store 的狀態(tài)而不調(diào)用任何action時(shí),它會(huì)非常有用。使用方法如下

<template>
	<div>
		<h1>vue組件</h1>
		{{ name }}
	</div>
</template>
<script setup>
//這里需要先引入
import { storeToRefs } from 'pinia'
import { useStudyStore } from '@/stores/study'
const store = useStudyStore();
//這樣解構(gòu)的屬性將保持響應(yīng)性
let { name } = storeToRefs(store);
// name.value 可以直接修改到Store中存儲(chǔ)的值
</script>

如果有多條數(shù)據(jù)要更新?tīng)顟B(tài),推薦使用$patch方式更新。因?yàn)镻inia的官方網(wǎng)站,已經(jīng)明確表示$ patch的方式是經(jīng)過(guò)優(yōu)化的,會(huì)加快修改速度,對(duì)程序的性能有很大的好處。

<template>
	<div>
		<h1>A組件</h1>
		{{ num}}
		{{ arr }}
		<button @click='btn'>按鈕</button>
	</div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStudyStore } from '@/stores/study'
const store = useStudyS
let { num,arr }  = storeToRefs(store);
const btn = ()=>{
	//批量更新
	store.$patch(state=>{
		state.num++;
		state.arr.push({name:'Ghmin'});
	})
}
</script>

actions:對(duì)state里數(shù)據(jù)變化的業(yè)務(wù)邏輯,需求不同,編寫(xiě)邏輯不同。說(shuō)白了就是修改state全局狀態(tài)數(shù)據(jù)的。

import { defineStore } from 'pinia'
export const useStudyStore = defineStore('studyId', {
  state: () => {
    return {
      num: 0
    }
  },
  getters:{},
  actions:{
  	changeNum( val ){
  		this.num+= val;
  	}
  }
})
<template>
	<div>
		<h1>使用actions</h1>
		{{ num}}
		<button @click='add'>加99</button>
	</div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStudyStore } from '@/stores/study'
const store = useStudyStore();
let { num}  = storeToRefs(store);
const add = ()=>{
	store.changeNum(10);
}
</script>

getters:和vuex的getters幾乎類似,用來(lái)監(jiān)視或者說(shuō)是計(jì)算狀態(tài)的變化的,有緩存的功能。

import { defineStore } from 'pinia'
export const useStudyStore = defineStore('studyId', {
  state: () => {
    return {
      num: 0,
    }
  },
  getters:{
  	numGetters(){
  		return this.counter + 999;
  	}
  },
  actions:{}
})
<template>
	<div>
	    <h1>getters的使用</h1>
		{{ num}}
		{{ numGetters}}
	</div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStudyStore } from '@/stores/study'
const store = useStudyStore();
let { num,numGetters}  = storeToRefs(store);
</script>

三.數(shù)據(jù)持久化存儲(chǔ)

使用pinia-plugin-persist實(shí)現(xiàn)數(shù)據(jù)持久化存儲(chǔ),具體使用請(qǐng)?zhí)D(zhuǎn)Pinia持久化存儲(chǔ)

http://www.dbjr.com.cn/python/292471hnu.htm

到此這篇關(guān)于快速搞懂Pinia及數(shù)據(jù)持久化存儲(chǔ)(詳細(xì)教程)的文章就介紹到這了,更多相關(guān)Pinia數(shù)據(jù)持久化存儲(chǔ)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論