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

vue中狀態(tài)管理器Pinia的用法詳解

 更新時(shí)間:2023年10月20日 14:36:45   作者:yuobey  
Pinia?是?Vue.js?的輕量級(jí)狀態(tài)管理庫(kù),最近很受歡迎,它使用?Vue?3?中的新反應(yīng)系統(tǒng)來構(gòu)建一個(gè)直觀且完全類型化的狀態(tài)管理庫(kù),下面就跟隨小編一起來學(xué)習(xí)一下它的具體使用吧

一、pinia介紹

Pinia 是 Vue.js 的輕量級(jí)狀態(tài)管理庫(kù),最近很受歡迎。它使用 Vue 3 中的新反應(yīng)系統(tǒng)來構(gòu)建一個(gè)直觀且完全類型化的狀態(tài)管理庫(kù)。

Pinia的成功可以歸功于其管理存儲(chǔ)數(shù)據(jù)的獨(dú)特功能(可擴(kuò)展性、存儲(chǔ)模塊組織、狀態(tài)變化分組、多存儲(chǔ)創(chuàng)建等)。

pinia優(yōu)點(diǎn):

  • 符合直覺,易于學(xué)習(xí)
  • 極輕, 僅有 1 KB
  • 模塊化設(shè)計(jì),便于拆分狀態(tài)
  • Pinia 沒有 mutations,統(tǒng)一在 actions 中操作 state,通過this.xx 訪問相應(yīng)狀態(tài)雖然可以直接操作 Store,但還是推薦在 actions 中操作,保證狀態(tài)不被意外改變
  • store 的 action 被調(diào)度為常規(guī)的函數(shù)調(diào)用,而不是使用 dispatch 方法或 MapAction 輔助函數(shù),這在 Vuex 中很常見
  • 支持多個(gè)Store
  • 支持 Vue devtools、SSR 和 webpack 代碼拆分

pinia缺點(diǎn):

不支持時(shí)間旅行和編輯等調(diào)試功能

二、安裝使用Pinia

在項(xiàng)目根目錄中打開終端,輸入以下命令

yarn add pinia@next 
# or with npm 
npm install pinia@next
// 該版本與Vue 3兼容,如果你正在尋找與Vue 2.x兼容的版本,請(qǐng)查看v1分支。

直接在main.js中使用

import { createApp } from "vue";
import App from "./App.vue";
import { setupStore } from "./store/index"; //引入pinia 緩存實(shí)例
const app = createApp(App);
setupStore(app); //把創(chuàng)建出來的vue實(shí)例app 傳入setupStore函數(shù) 進(jìn)行掛載
app.mount("#app");

創(chuàng)建store文件夾,一般的項(xiàng)目我們可以不區(qū)分模塊,但是按照習(xí)慣,個(gè)人覺得區(qū)分模塊會(huì)讓代碼閱讀起來更加清晰

如:

store/index.js

import { createPinia } from "pinia";
const store = createPinia();
export function setupStore(app) {
    app.use(store);
}
export { store };

創(chuàng)建模塊app.js代碼:

import { defineStore } from "pinia";
export const useAppStore = defineStore({
  // id is required so that Pinia can connect the store to the devtools
  id: "app",
  state: () => ({
    clientWidth: "",
    clientHeight: ""
  }),
  getters: {
    getClientWidth() {
      return this.clientWidth;
    },
    getClientHeight() {
      return this.clientHeight;
    }
  },
  actions: {
    setClientWidth(payload) {
      try {
        setTimeout(() => {
          this.clientWidth = payload;
        }, 2000);
      } catch (error) {}
    },
    setClientHeight(payload) {
      try {
        setTimeout(() => {
          this.clientHeight = payload;
        }, 2000);
      } catch (error) {}
    }
  }
});

使用其實(shí)很簡(jiǎn)單,只要在對(duì)應(yīng)組件引入對(duì)應(yīng)的模塊

如:

<template>
  {{ width }}
</template>

<script>
import { ref, reactive, onMounted,computed } from "vue";
import { useAppStore } from "@/store/modules/app";

export default {
  name: "App",
  setup() {
    const appStore = useAppStore();
    const width = computed(() => {
      return appStore.clientWidth;
    });
    onMounted(() => {
      appStore.setClientWidth(200);
    });
    return {
      width,
    };
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

pinia還可以在當(dāng)前模塊很直觀的使用其他模塊的方法

如:

import { defineStore } from "pinia";
import { useOtherStore } from "@/store/modules/other.js";
export const useAppStore = defineStore({
  // id is required so that Pinia can connect the store to the devtools
  id: "app",
  //state必須是一個(gè)返回對(duì)象的函數(shù)
  state: () => ({
    clientWidth: "",
    clientHeight: ""
  }),
  getters: {
    getClientWidth() {
      return this.clientWidth;
    },
    getClientHeight() {
      return this.clientHeight;
    },
       // 使用其它 Store
    otherStoreCount(state) {
      // 這里是其他的 Store,調(diào)用獲取 Store,就和在 setup 中一樣
      const otherStore = useOtherStore();
      return otherStore.count;
    },
  },
  actions: {
    setClientWidth(payload) {
      try {
        setTimeout(() => {
          this.clientWidth = payload;
        }, 2000);
      } catch (error) {}
    },
    setClientHeight(payload) {
      try {
        setTimeout(() => {
          this.clientHeight = payload;
        }, 2000);
      } catch (error) {}
    },
    // 使用其它模塊的action
    setOtherStoreCount(state) {
      // 這里是其他的 Store,調(diào)用獲取 Store,就和在 setup 中一樣
      const otherStore = useOtherStore();
         otherStore.setMethod()
    },
  }
});

三、Pinia 中Actions改變state的幾種方法

我們?cè)诮M件中,引入對(duì)應(yīng)模塊后如:

<template>
  {{ width }}
</template>

<script>
import { ref, reactive, onMounted,computed } from "vue";
import { useAppStore } from "@/store/modules/app";

export default {
  name: "App",
  setup() {
    const appStore = useAppStore();
    const width = computed(() => {
      return appStore.clientWidth;
    });
    onMounted(() => {
      appStore.setClientWidth(200);
    });
    //演示三種方法修改state
    const changeAppstoreStateClick = () => {
     // 方式一:直接修改 -> 'direct'
      appStore.clientWidth += 400;
     // 方式二:patch對(duì)象方式 -> 'patch object',調(diào)用$patch 傳入要修改的鍵和val
      appStore.$patch({
        clientWidth: appStore.clientWidth + 400,
      });
      // 方式三:patch函數(shù)方式 -> 'patch function',可鍵入語句,執(zhí)行復(fù)雜邏輯
      appStore.$patch((state) => {
        state.clientWidth += 400;
      });

    };
    return {
      width,
      changeAppstoreStateClick
    };
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

以上就是vue中狀態(tài)管理器Pinia的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于vue狀態(tài)管理器Pinia的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 在vue項(xiàng)目中引用Antv G2,以餅圖為例講解

    在vue項(xiàng)目中引用Antv G2,以餅圖為例講解

    這篇文章主要介紹了在vue項(xiàng)目中引用Antv G2,以餅圖為例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue.js封裝switch開關(guān)組件的操作

    vue.js封裝switch開關(guān)組件的操作

    這篇文章主要介紹了vue.js封裝switch開關(guān)組件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue中使用v-model完成組件間的通信

    vue中使用v-model完成組件間的通信

    vue中有一個(gè)很神奇的東西叫v-model,它可以完成我們的需求。,本文重點(diǎn)給大家介紹vue中使用v-model完成組件間的通信,需要的朋友可以參考下
    2019-08-08
  • vue fetch中的.then()的正確使用方法

    vue fetch中的.then()的正確使用方法

    這篇文章主要介紹了vue fetch中的.then()的正確使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • vue項(xiàng)目創(chuàng)建并引入餓了么elementUI組件的步驟

    vue項(xiàng)目創(chuàng)建并引入餓了么elementUI組件的步驟

    這篇文章主要介紹了vue項(xiàng)目創(chuàng)建并引入餓了么elementUI組件的步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-04-04
  • vue登錄成功之后的token處理詳細(xì)步驟

    vue登錄成功之后的token處理詳細(xì)步驟

    Token是身份驗(yàn)證后服務(wù)端返回的令牌,常用于訪問授權(quán)API和頁面權(quán)限校驗(yàn),Token數(shù)據(jù)可存儲(chǔ)在本地或Vuex中,本地存儲(chǔ)可實(shí)現(xiàn)數(shù)據(jù)持久化,這篇文章主要介紹了vue登錄成功之后的token處理詳細(xì)步驟,需要的朋友可以參考下
    2024-10-10
  • vue3使用videojs播放m3u8格式視頻教程

    vue3使用videojs播放m3u8格式視頻教程

    m3u8是一種基于HTTP Live Streaming(HLS)文件視頻格式,它主要是存放整個(gè)視頻的基本信息和分片(Segment)組成,下面這篇文章主要給大家介紹了關(guān)于vue3使用videojs播放m3u8格式視頻的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • vue使用自定義icon圖標(biāo)的方法

    vue使用自定義icon圖標(biāo)的方法

    這篇文章主要介紹了vue使用自定義的icon圖標(biāo)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • Vue v-text指令簡(jiǎn)單使用方法示例

    Vue v-text指令簡(jiǎn)單使用方法示例

    這篇文章主要介紹了Vue v-text指令簡(jiǎn)單使用方法,結(jié)合實(shí)例形式分析了v-text指令文本輸出顯示簡(jiǎn)單操作技巧,需要的朋友可以參考下
    2019-09-09
  • 原生Vue 實(shí)現(xiàn)右鍵菜單組件功能

    原生Vue 實(shí)現(xiàn)右鍵菜單組件功能

    這篇文章主要介紹了Vue 原生實(shí)現(xiàn)右鍵菜單組件功能,本文給大家擴(kuò)展知識(shí)點(diǎn)vue點(diǎn)擊菜單以外區(qū)域,隱藏菜單操作,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-12-12

最新評(píng)論

佛冈县| 霍林郭勒市| 长岛县| 新营市| 辽宁省| 望都县| 同德县| 慈溪市| 泗阳县| 平陆县| 宽甸| 互助| 偃师市| 新巴尔虎右旗| 旬邑县| 富民县| 荔浦县| 通海县| 米泉市| 平南县| 台中市| 乌鲁木齐县| 浠水县| 南充市| 浦城县| 乌审旗| 鲁山县| 武平县| 尼木县| 元阳县| 新昌县| 沿河| 芮城县| 阜南县| 龙南县| 当涂县| 忻城县| 大兴区| 罗田县| 缙云县| 崇礼县|