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

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

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

一、pinia介紹

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

Pinia的成功可以歸功于其管理存儲數(shù)據(jù)的獨(dú)特功能(可擴(kuò)展性、存儲模塊組織、狀態(tài)變化分組、多存儲創(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 中很常見
  • 支持多個Store
  • 支持 Vue devtools、SSR 和 webpack 代碼拆分

pinia缺點(diǎn):

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

二、安裝使用Pinia

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

yarn add pinia@next 
# or with npm 
npm install pinia@next
// 該版本與Vue 3兼容,如果你正在尋找與Vue 2.x兼容的版本,請查看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í)慣,個人覺得區(qū)分模塊會讓代碼閱讀起來更加清晰

如:

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í)很簡單,只要在對應(yīng)組件引入對應(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必須是一個返回對象的函數(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的幾種方法

我們在組件中,引入對應(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對象方式 -> '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的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟

    webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟

    這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下
    2023-11-11
  • vue多頁面開發(fā)和打包正確處理方法

    vue多頁面開發(fā)和打包正確處理方法

    這篇文章主要介紹了vue多頁面開發(fā)和打包的正確處理方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-04-04
  • vue+element創(chuàng)建動態(tài)的form表單及動態(tài)生成表格的行和列

    vue+element創(chuàng)建動態(tài)的form表單及動態(tài)生成表格的行和列

    這篇文章主要介紹了vue+element創(chuàng)建動態(tài)的form表單及動態(tài)生成表格的行和列 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • vue同一瀏覽器登錄多賬號處理方案

    vue同一瀏覽器登錄多賬號處理方案

    項(xiàng)目在線上會遇到管理員類似權(quán)限比較大的用戶,會在同一瀏覽器登陸多個賬號,遇到這樣的問題如何處理呢,下面小編給大家介紹vue同一瀏覽器登錄多賬號處理方法,感興趣的朋友一起看看吧
    2024-01-01
  • Vue前端導(dǎo)出頁面為PDF文件的最佳方案

    Vue前端導(dǎo)出頁面為PDF文件的最佳方案

    這篇文章主要介紹了前端導(dǎo)出PDF方案,通過html2canvas和jsPDF實(shí)現(xiàn)單頁導(dǎo)出,利用iframe分批處理列表頁數(shù)據(jù)并打包ZIP,兼顧性能與樣式還原,有效減輕服務(wù)端壓力,需要的朋友可以參考下
    2025-07-07
  • vue3+uniapp 上傳附件的操作代碼

    vue3+uniapp 上傳附件的操作代碼

    uni-file-picker搭配uni.uploadFile在使用問題上踩了不少坑,我至今還是沒辦法在不改uniapp源碼基礎(chǔ)上實(shí)現(xiàn)限制重復(fù)文件的上傳,這篇文章介紹vue3+uniapp 上傳附件的操作代碼,感興趣的朋友一起看看吧
    2024-01-01
  • 使用Vue3和Vite實(shí)現(xiàn)對低版本瀏覽器的兼容

    使用Vue3和Vite實(shí)現(xiàn)對低版本瀏覽器的兼容

    在使用Vite和Vue3構(gòu)建的JavaScript項(xiàng)目中,確保對低版本瀏覽器的兼容性是一個重要的考慮因素,以下是一些具體的解決方案和步驟,可以幫助你實(shí)現(xiàn)這一目標(biāo),需要的朋友可以參考下
    2024-11-11
  • 前端小技能之Vue集成百度離線地圖功能總結(jié)

    前端小技能之Vue集成百度離線地圖功能總結(jié)

    最近項(xiàng)目里集成了百度地圖的一些功能,所以下面這篇文章主要給大家介紹了關(guān)于前端小技能之Vue集成百度離線地圖功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • vue3數(shù)據(jù)監(jiān)聽watch/watchEffect的示例代碼

    vue3數(shù)據(jù)監(jiān)聽watch/watchEffect的示例代碼

    我們都知道監(jiān)聽器的作用是在每次響應(yīng)式狀態(tài)發(fā)生變化時觸發(fā),在組合式?API?中,我們可以使用?watch()函數(shù)和watchEffect()函數(shù),下面我們來看下vue3如何進(jìn)行數(shù)據(jù)監(jiān)聽watch/watchEffect,感興趣的朋友一起看看吧
    2023-02-02
  • 基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動功能的代碼

    基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動功能的代碼

    BetterScroll 是一款重點(diǎn)解決移動端(已支持 PC)各種滾動場景需求的插件,BetterScroll 是使用純 JavaScript 實(shí)現(xiàn)的,這意味著它是無依賴的。本文基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動功能,感興趣的朋友跟隨小編一起看看吧
    2020-05-05

最新評論