亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

一.安裝及使用Pinia

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

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應(yīng)用實例
// 實例化 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è)務(wù)邏輯,需求不同,編寫邏輯不同。說白了就是修改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)是不推薦的,因為它破壞了響應(yīng)性。

而為了從 Store 中提取屬性,同時保持其響應(yīng)性,這里需要使用storeToRefs(),它將為每個響應(yīng)性屬性創(chuàng)建引用。當(dā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)的屬性將保持響應(yīng)性
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è)務(wù)邏輯,需求不同,編寫邏輯不同。說白了就是修改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://chabaoo.cn/python/292471hnu.htm

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

相關(guān)文章

  • 深入淺析Vue.js中 computed和methods不同機制

    深入淺析Vue.js中 computed和methods不同機制

    這篇文章給大家介紹了Vue.js中 computed和methods不同機制,在vue.js中,methods和computed兩種方式來動態(tài)當(dāng)作方法使用,文中還給大家提到了computed和methods的區(qū)別,感興趣的朋友一起看看吧
    2018-03-03
  • vue項目中圖片選擇路徑位置static或assets的區(qū)別及說明

    vue項目中圖片選擇路徑位置static或assets的區(qū)別及說明

    這篇文章主要介紹了vue項目中圖片選擇路徑位置static或assets的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue實現(xiàn)指令式動態(tài)追加小球動畫組件的步驟

    Vue實現(xiàn)指令式動態(tài)追加小球動畫組件的步驟

    這篇文章主要介紹了Vue實現(xiàn)指令式動態(tài)追加小球動畫組件的步驟,幫助大家更好的理解和實用vue,感興趣的朋友可以了解下
    2020-12-12
  • vue3中使用pinia(大菠蘿)狀態(tài)管理倉庫的項目實踐

    vue3中使用pinia(大菠蘿)狀態(tài)管理倉庫的項目實踐

    本文主要介紹了vue3中使用pinia(大菠蘿)狀態(tài)管理倉庫,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • vue實現(xiàn)PC端錄音功能的實例代碼

    vue實現(xiàn)PC端錄音功能的實例代碼

    這篇文章主要介紹了vue實現(xiàn)PC端錄音功能的實例代碼,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • vue常用知識點整理

    vue常用知識點整理

    Vue是一套用于構(gòu)建用戶界面的漸進式JavaScript框架。這篇文章整理了vue中的常用知識點,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 淺談Vue中render中的h箭頭函數(shù)

    淺談Vue中render中的h箭頭函數(shù)

    今天小編就為大家分享一篇淺談Vue中render中的h箭頭函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue2.0 實現(xiàn)移動端圖片上傳功能

    Vue2.0 實現(xiàn)移動端圖片上傳功能

    本文主要介紹VUE2.0圖片上傳功能的實現(xiàn)。原理是通過js控制和input標(biāo)簽的方式完成這一效果,無需加載其他組件。具體實例大家大家參考下本文
    2018-05-05
  • Vue 組件的掛載與父子組件的傳值實例

    Vue 組件的掛載與父子組件的傳值實例

    這篇文章主要介紹了Vue 組件的掛載與父子組件的傳值實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 新版vue-cli模板下本地開發(fā)環(huán)境使用node服務(wù)器跨域的方法

    新版vue-cli模板下本地開發(fā)環(huán)境使用node服務(wù)器跨域的方法

    這篇文章主要介紹了新版vue-cli模板下本地開發(fā)環(huán)境使用node服務(wù)器跨域的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論