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

Vue pinia模塊化全局注冊詳解

 更新時間:2023年02月02日 08:52:30   作者:IN~Trying  
這篇文章主要介紹了Vue pinia模塊化全局注冊,Pinia是Vue.js團隊成員專門為Vue開發(fā)的一個全新的狀態(tài)管理庫,并且已經被納入官方github

Vue3中對pinia模塊化全局注冊

項目小使用簡易的方式還不覺得,但是項目大了后就會發(fā)現(xiàn)引入的東西有些重復了

安裝

yarn add pinia
# or with npm
npm install pinia

掛載實例

main.ts中 掛載pinia實例

import { createPinia } from "pinia";
...
const pinia = createPinia()
app.use(pinia);

話不多說,直接貼代碼

在scr下創(chuàng)建stores文件夾,創(chuàng)建index.ts文件

// 使用options API模式定義,vue2的組件模型形式類似
// import useDemoStore  from "./modules/addNumber";
// export interface PiniaStore {
//     useDemoStore:ReturnType<typeof useDemoStore>
// }
// const piniaStore: PiniaStore = {} as PiniaStore;
// /**
//  * 注冊app狀態(tài)庫
//  */
// export const registerStore = () => {
//     piniaStore.useDemoStore = useDemoStore()
// };
// export default piniaStore;
// 使用setup模式定義
import { useDemoStore } from "./modules/addNumber";
// import textContentStore from "./modules/textContent";  //單一個方法
import { textContentStore, usefruitStore } from "./modules/textContent"; //多個不同需緩存的方法
export interface PiniaStore {
  useDemoStore: ReturnType<typeof useDemoStore>;
  textContentStore: ReturnType<typeof textContentStore>;
  usefruitStore: ReturnType<typeof usefruitStore>;
}
const piniaStore: PiniaStore = {} as PiniaStore;
/**
 * 注冊app狀態(tài)庫
 */
export const registerStore = () => {
  piniaStore.useDemoStore = useDemoStore();
  piniaStore.textContentStore = textContentStore();
  piniaStore.usefruitStore = usefruitStore();
};
export default piniaStore;

scr/stores/modules/

新建你的store.ts
這里使用了兩種不同的數(shù)據(jù)持久化插件,如果不需要可忽略插件

1、pinia-plugin-persist 插件的數(shù)據(jù)持久化使用

2、pinia-plugin-persistedstate插件

兩個插件的屬性使用不一樣,需注意
代碼使用了兩個不同的寫法,

1、使用options API模式定義,vue2的組件模型形式類似

2、使用setup模式定義

主要是用作全局注冊

import { defineStore } from "pinia";
import { ref } from "vue";
//  pinia-plugin-persist 插件的數(shù)據(jù)持久化使用
export const textContentStore = defineStore({
  id: "goods",
  state: () => {
    return {
      fruit: "蘋果",
      price: 15,
    };
  },
  actions: {
    changeName() {
      this.fruit = "雪梨";
    },
    changePrice(val: number) {
      this.price = val;
    },
  },
  // 開啟數(shù)據(jù)緩存
  persist: {
    enabled: true,
    key: "goods",
    //   strategies: [
    //     {
    //       storage: localStorage,
    //       paths: ['accessToken']
    //     },
    strategies: [
      //   自定義存儲到 sessionStorage 和 localStorage
      { key: "fruit", storage: sessionStorage, paths: ["fruit"] },
      { key: "price", storage: localStorage, paths: ["price"] },
    ],
  },
});
export const usefruitStore = defineStore(
  "goods1",
  () => {
    const fruit1 = ref<string>("香蕉");
    const price1 = ref<number>(10);
    function changeName1() {
      fruit1.value = "雪梨";
    }
    function changePrice1(val: number) {
      price1.value = val;
    }
    return { fruit1, price1, changeName1, changePrice1 };
  },
  {
    //持久化存儲配置 ,必須同步詳情可看官方說明文檔
    persist: {
      key: "_pinia_price1",
      storage: sessionStorage,
      paths: ["fruit1"],
    },
  }
);
// export const textContentStore = defineStore(
//   "counter",
//   () => {
//     const fruit = ref<string>("蘋果");
//     const price = ref<number>(100);
//     function changeName() {
//       fruit.value = "雪梨";
//     }
//     function changePrice(val:number) {
//         price.value = val
//     }
//     return { fruit, price, changeName, changePrice, };
//   },
// //   {
// //     //持久化存儲配置 ,必須同步詳情可看官方說明文檔
// //     persist: {
// //       key: "_pinia_fruit",
// //       storage: sessionStorage,
// //       paths: ["fruit"],
// //     },
// //   }
// );

頁面

到頁面上的使用

<h2>水果</h2>
    <h3>名稱1:{{ fruit }}---價格:{{ price }}</h3>
    <button @click="changeName">修改名稱</button>
    <button @click="ChangePrice">修改價格</button>
    --------------------------------------------
    <h3>名稱2:{{ fruit1 }}---價格:{{ price1 }}</h3>
    <button @click="changeName1">修改名稱1</button>
    <button @click="changePrice1(120)">修改價格1</button>
import PiniaStore from "../stores";
import { storeToRefs } from "pinia";
// setup composition API模式
const { fruit, price } = storeToRefs(PiniaStore.textContentStore);
const { changeName, changePrice } = PiniaStore.textContentStore;
const { fruit1, price1 } = storeToRefs(PiniaStore.usefruitStore);

相對來說項目小的話沒什么必要做全局,但是項目大了可能這樣會好維護一些

當然也會有更好的方式,只是我沒發(fā)現(xiàn)

最后補充

打包解耦

到這里還不行,為了讓appStore實例與項目解耦,在構建時要把appStore抽取到公共chunk,在vite.config.ts做如下配置

build: {
      outDir: "dist",
      rollupOptions: {
        output: {
          manualChunks(id) {
            //靜態(tài)資源分拆打包
            ...其他配置
            // 將pinia的全局庫實例打包進vendor,避免和頁面一起打包造成資源重復引入
            if (id.includes(resolve(__dirname, "/src/store/index.ts"))) {
              return "vendor";
            }
          },
        },
      },
   }

到此這篇關于Vue pinia模塊化全局注冊詳解的文章就介紹到這了,更多相關Vue pinia模塊化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Vue axios 跨域請求無法帶上cookie的解決

    Vue axios 跨域請求無法帶上cookie的解決

    這篇文章主要介紹了Vue axios 跨域請求無法帶上cookie的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue內嵌iframe跨域通信的實例代碼

    vue內嵌iframe跨域通信的實例代碼

    這篇文章主要介紹了vue內嵌iframe跨域通信,主要介紹了Vue組件中如何引入iframe,vue如何獲取iframe對象以及iframe內的window對象,結合實例代碼給大家介紹的非常詳細需要的朋友可以參考下
    2022-11-11
  • vue項目部署到nginx/tomcat服務器的實現(xiàn)

    vue項目部署到nginx/tomcat服務器的實現(xiàn)

    這篇文章主要介紹了vue項目部署到nginx/tomcat服務器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • vue中的依賴注入provide和inject簡單介紹

    vue中的依賴注入provide和inject簡單介紹

    這篇文章主要介紹了vue中的依賴注入provide和inject簡單介紹,provide 選項允許我們指定我們想要提供給后代組件的數(shù)據(jù)/方法,本文通過組價刷新的案列給大家詳細講解,需要的朋友可以參考下
    2022-11-11
  • vue使用this.$message不生效的部分原因及解決方案

    vue使用this.$message不生效的部分原因及解決方案

    這篇文章主要介紹了vue使用this.$message不生效的部分原因及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Vue自定義元素身上的右鍵事件問題

    Vue自定義元素身上的右鍵事件問題

    這篇文章主要介紹了Vue自定義元素身上的右鍵事件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • el-tree?loadNode懶加載的實現(xiàn)

    el-tree?loadNode懶加載的實現(xiàn)

    本文主要介紹了el-tree?loadNode懶加載的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Vuex與Vue router的使用詳細講解

    Vuex與Vue router的使用詳細講解

    在看這篇文章的幾點要求:需要你先知道Vuex與Vue-Router是個什么東西,用來解決什么問題,以及它的基本使用。如果你還不懂的話,建議上官網(wǎng)了解下Vuex與Vue-Router的基本使用后再回來看這篇文章
    2022-11-11
  • 單頁面Vue頁面刷新出現(xiàn)閃爍問題及解決

    單頁面Vue頁面刷新出現(xiàn)閃爍問題及解決

    這篇文章主要介紹了單頁面Vue頁面刷新出現(xiàn)閃爍問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue中如何讓子組件修改父組件數(shù)據(jù)

    vue中如何讓子組件修改父組件數(shù)據(jù)

    這篇文章主要介紹了vue中子組件修改父組件數(shù)據(jù)的相關資料,文中介紹了vue中watch的認識,關于子組件修改父組件屬性認識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06

最新評論