最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3中引入Pinia存儲庫使用示例詳解

 更新時間:2023年03月27日 11:28:17   作者:HqL丶1024  
這篇文章主要介紹了Vue3中引入Pinia存儲庫使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

直達pinia官網(wǎng)

1.用自己最喜歡的方式安裝

yarn add pinia
# 或者使用 npm
npm install pinia

2.main.js引入

import { createApp } from 'vue'
import App from './App.vue'
 
const app=createApp(App)
import { createPinia } from 'pinia' //引入pinia
app.use(createPinia())
 
app.mount('#app') 

3.創(chuàng)建store文件并配置內(nèi)部的index.js文件

import { defineStore } from 'pinia' //引入pinia
 
//這里官網(wǎng)是單獨導(dǎo)出  是可以寫成默認導(dǎo)出的  官方的解釋為大家一起約定倉庫用use打頭的單詞 固定統(tǒng)一小倉庫的名字不易混亂
export const useCar=defineStore("test",{ 
    state: () =>{
        return  ({
            msg:"這是pinia",
            name:"小小",
            age:18
            }) //為了避免出錯,返回的值用()包起來
    } 
})

4.組件使用方法

<template>
    <h1>{{store.msg}}{{store.name}}{{store.age}}</h1>
    <button @click="modify">修改store.name</button>
</template>
 
<script>
import { defineComponent, ref } from 'vue';
import {
  reactive,
  toRefs,
  onMounted,
  onActivated
} from "vue";
import {useCar} from "../store/index.js" //將之前配置的pinia文件夾中的index.js文件引入
export default defineComponent({
  let store=useCar() //接收
  setup() {
    const state = reactive({
      testMsg: "原始值",
    });
    onMounted(async () => {});
    onActivated(() => {})
    const methods = {
        modify(){
             store.name = state.testMsg //修改pinia里面的數(shù)據(jù)
             console.log(store.name)
        }
    };
    return {
      ...toRefs(state),
      ...methods,
    };
  }
})
</script>

5.重置store.$reset()

<template>
    <h1>{{store.msg}}{{store.name}}{{store.age}}</h1>
    <button @click="reset">重置store.name</button>
</template>
 
<script>
import { defineComponent, ref } from 'vue';
import {
  reactive,
  toRefs,
  onMounted,
  onActivated
} from "vue";
import {useCar} from "../store/index.js" //將之前配置的pinia文件夾中的index.js文件引入
export default defineComponent({
  let store=useCar() //接收
  setup() {
    const state = reactive({
      testMsg: "原始值",
    });
    onMounted(async () => {});
    onActivated(() => {})
    const methods = {
        reset(){
             store.$reset() //重置pinia里面的數(shù)據(jù)
             console.log(store.name)
        }
    };
    return {
      ...toRefs(state),
      ...methods,
    };
  }
})
</script>

6.群體修改store.$patch,可以將pinia的數(shù)據(jù)進行同一修改

特點:批量修改但狀態(tài)只刷新一次

<template>
    <h1>{{store.msg}}{{store.name}}{{store.age}}</h1>
    <button @click="modify">修改store.name</button>
    <button @click="reset">重置store.name</button>
    <button @click="allModify">群體修改store.name</button>
</template>
 
<script>
import { defineComponent, ref } from 'vue';
import {
  reactive,
  toRefs,
  onMounted,
  onActivated
} from "vue";
import {useCar} from "../store/index.js" //將之前配置的pinia文件夾中的index.js文件引入
export default defineComponent({
  let store=useCar() //接收
  setup() {
    const state = reactive({
      testMsg: "原始值",
    });
    onMounted(async () => {});
    onActivated(() => {})
    const methods = {
        modify(){
             store.name = state.testMsg //修改pinia里面的數(shù)據(jù)
             console.log(store.name)
        },
        reset(){
             store.$reset() //重置pinia里面的數(shù)據(jù)
             console.log(store.name)
        },
        allModify(){
             store.$patch({
              name:"花花",
              age:20,
            })
        }
    };
    return {
      ...toRefs(state),
      ...methods,
    };
  }
})
</script>

7.訂閱修改

//可以通過 store 的 $subscribe() 方法查看狀態(tài)及其變化,通過patch修改狀態(tài)時就會觸發(fā)一次
store.$subscribe((mutation, state) => { 
  // 每當它發(fā)生變化時,將整個狀態(tài)持久化到本地存儲
  localStorage.setItem('hello', JSON.stringify(state))
})

8.Getter

Getter 完全等同于 Store 狀態(tài)的 計算值。 它們可以用 defineStore() 中的 getters 屬性定義。 他們接收“狀態(tài)”作為第一個參數(shù)以鼓勵箭頭函數(shù)的使用:(ps:雖然鼓勵但是依然提供了非es6玩家的使用方式 內(nèi)部可以用this代表state)

//store/index.js文件
export const useStore = defineStore('main', {
  state: () => ({
    counter: 0,
  }),
  getters: {
    doubleCount: (state) => state.counter * 2,
  },
})
 
//組件中直接使用:  <p>Double count is {{ store.doubleCount }}</p>

9.Actions

在pinia中沒有提供mutaion 因為Actions就夠了(它可以異步同步的統(tǒng)一修改狀態(tài))之所以提供這個功能 就是為了項目中的公共修改狀態(tài)的業(yè)務(wù)統(tǒng)一

export const useStore = defineStore('main', {
  state: () => ({
    counter: 0,
  }),
  actions: {
    increment() {
      this.counter++//1.有this
    },
    randomizeCounter(state) {//2.有參數(shù)   想用哪個用哪個
      state.counter = Math.round(100 * Math.random())
    },
    randomizeCounter(state) {
        //異步函數(shù).接口成功賦值
     ajax.hplBaseApi("app/productSelection/categoryRows", {}, "post").then((res) => {
        state.counter = res.data.length
     });
    }
  },
})
 
//組件中的使用:
  setup() {
    const store = useStore()
    store.increment()
    store.randomizeCounter()
  }

到此這篇關(guān)于Vue3中引入Pinia存儲庫使用的文章就介紹到這了,更多相關(guān)Vue3存儲庫Pinia使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue如何處理Axios多次請求數(shù)據(jù)顯示問題

    Vue如何處理Axios多次請求數(shù)據(jù)顯示問題

    這篇文章主要介紹了Vue如何處理Axios多次請求數(shù)據(jù)顯示問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue2.0在IE11版本瀏覽器中的兼容性問題

    Vue2.0在IE11版本瀏覽器中的兼容性問題

    這篇文章主要介紹了Vue2.0在IE11版本瀏覽器中的兼容性問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • axios取消請求CancelToken的用法示例代碼

    axios取消請求CancelToken的用法示例代碼

    Axios提供了取消請求的功能,可以通過使用CancelToken來實現(xiàn),這篇文章主要給大家介紹了關(guān)于axios取消請求CancelToken的用法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-10-10
  • Vue+Jwt+SpringBoot+Ldap完成登錄認證的示例代碼

    Vue+Jwt+SpringBoot+Ldap完成登錄認證的示例代碼

    本篇文章主要介紹了Vue+Jwt+SpringBoot+Ldap完成登錄認證的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • vue設(shè)置代理不起作用問題及解決

    vue設(shè)置代理不起作用問題及解決

    這篇文章主要介紹了vue設(shè)置代理不起作用問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue-dialog的彈出層組件

    vue-dialog的彈出層組件

    這篇文章主要為大家詳細介紹了vue-dialog的彈出層組件,可以通過npm引用的組件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 解決vue報錯:Do?not?mutate?vuex?store?state?outside?mutation?handlers問題

    解決vue報錯:Do?not?mutate?vuex?store?state?outside?mutati

    這篇文章主要介紹了解決vue報錯:Do?not?mutate?vuex?store?state?outside?mutation?handlers問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 淺談在不使用ssr的情況下解決Vue單頁面SEO問題(2)

    淺談在不使用ssr的情況下解決Vue單頁面SEO問題(2)

    這篇文章主要介紹了淺談在不使用ssr的情況下解決Vue單頁面SEO問題(2),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • Vue利用高德地圖API實現(xiàn)實時天氣

    Vue利用高德地圖API實現(xiàn)實時天氣

    這篇文章主要為大家詳細介紹了Vue如何利用高德地圖API實現(xiàn)實時天氣,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • vue 實現(xiàn)特定條件下綁定事件

    vue 實現(xiàn)特定條件下綁定事件

    今天小編就為大家分享一篇vue 實現(xiàn)特定條件下綁定事件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11

最新評論

彝良县| 桐柏县| 黄山市| 且末县| 华阴市| 尖扎县| 丰原市| 贵溪市| 崇左市| 浦江县| 防城港市| 新竹市| 龙陵县| 德钦县| 镇康县| 长治县| 同心县| 光泽县| 革吉县| 阳原县| 宝应县| 无极县| 柳州市| 甘肃省| 宁化县| 丹棱县| 晋州市| 时尚| 康马县| 日喀则市| 岐山县| 九江县| 汉寿县| 莲花县| 林甸县| 钟祥市| 淮滨县| 南漳县| 隆化县| 和平区| 平南县|