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

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

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

一.安裝及使用Pinia

1.安裝Pinia兩種方式都可,根據(jù)個人習慣來

npm install pinia
yarn add pinia

2.在main.ts 中引入并掛載到根實例

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

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

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

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

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

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

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

第一種Options式寫法:

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

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

第二種Setup式寫法:

import { defineStore } from 'pinia'
//  `defineStore()` 的返回值命名最好使用 store 的名字,同時以 `use` 開頭且以 `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ù),無需像vuex一樣通過mutations才可以修改,所以上面的let { name } = store 這種解構(gòu)是不推薦的,因為它破壞了響應性。

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

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

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

<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:對state里數(shù)據(jù)變化的業(yè)務邏輯,需求不同,編寫邏輯不同。說白了就是修改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幾乎類似,用來監(jiān)視或者說是計算狀態(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ù)持久化存儲

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

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

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

相關(guān)文章

最新評論