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

Vue pinia模塊化全局注冊(cè)詳解

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

Vue3中對(duì)pinia模塊化全局注冊(cè)

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

安裝

yarn add pinia
# or with npm
npm install pinia

掛載實(shí)例

main.ts中 掛載pinia實(shí)例

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

話不多說(shuō),直接貼代碼

在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;
// /**
//  * 注冊(cè)app狀態(tài)庫(kù)
//  */
// export const registerStore = () => {
//     piniaStore.useDemoStore = useDemoStore()
// };
// export default piniaStore;
// 使用setup模式定義
import { useDemoStore } from "./modules/addNumber";
// import textContentStore from "./modules/textContent";  //單一個(gè)方法
import { textContentStore, usefruitStore } from "./modules/textContent"; //多個(gè)不同需緩存的方法
export interface PiniaStore {
  useDemoStore: ReturnType<typeof useDemoStore>;
  textContentStore: ReturnType<typeof textContentStore>;
  usefruitStore: ReturnType<typeof usefruitStore>;
}
const piniaStore: PiniaStore = {} as PiniaStore;
/**
 * 注冊(cè)app狀態(tài)庫(kù)
 */
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插件

兩個(gè)插件的屬性使用不一樣,需注意
代碼使用了兩個(gè)不同的寫(xiě)法,

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

2、使用setup模式定義

主要是用作全局注冊(cè)

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

頁(yè)面

到頁(yè)面上的使用

<h2>水果</h2>
    <h3>名稱1:{{ fruit }}---價(jià)格:{{ price }}</h3>
    <button @click="changeName">修改名稱</button>
    <button @click="ChangePrice">修改價(jià)格</button>
    --------------------------------------------
    <h3>名稱2:{{ fruit1 }}---價(jià)格:{{ price1 }}</h3>
    <button @click="changeName1">修改名稱1</button>
    <button @click="changePrice1(120)">修改價(jià)格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);

相對(duì)來(lái)說(shuō)項(xiàng)目小的話沒(méi)什么必要做全局,但是項(xiàng)目大了可能這樣會(huì)好維護(hù)一些

當(dāng)然也會(huì)有更好的方式,只是我沒(méi)發(fā)現(xiàn)

最后補(bǔ)充

打包解耦

到這里還不行,為了讓appStore實(shí)例與項(xiàng)目解耦,在構(gòu)建時(shí)要把a(bǔ)ppStore抽取到公共chunk,在vite.config.ts做如下配置

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

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

相關(guān)文章

  • uni-app中app和webview的h5通信簡(jiǎn)單步驟

    uni-app中app和webview的h5通信簡(jiǎn)單步驟

    這篇文章主要介紹了如何在nvue頁(yè)面中使用webview組件,并詳細(xì)介紹了如何在h5項(xiàng)目中安裝和配置npmiy_uniwebview插件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • 使用vue2實(shí)現(xiàn)帶地區(qū)編號(hào)和名稱的省市縣三級(jí)聯(lián)動(dòng)效果

    使用vue2實(shí)現(xiàn)帶地區(qū)編號(hào)和名稱的省市縣三級(jí)聯(lián)動(dòng)效果

    我們知道省市區(qū)縣都有名稱和對(duì)應(yīng)的數(shù)字唯一編號(hào),使用編號(hào)可以更方便查詢以及程序處理,我們今天來(lái)了解一下使用vue2來(lái)實(shí)現(xiàn)常見(jiàn)的省市區(qū)下拉聯(lián)動(dòng)選擇效果,需要的朋友可以參考下
    2018-11-11
  • 探秘Vue異步更新機(jī)制中nextTick的原理與實(shí)現(xiàn)

    探秘Vue異步更新機(jī)制中nextTick的原理與實(shí)現(xiàn)

    nextTick?是?Vue?提供的一個(gè)重要工具,它的作用主要體現(xiàn)在幫助我們更好地處理異步操作,下面就跟隨小編一起來(lái)探索一下nextTick的原理與實(shí)現(xiàn)吧
    2024-02-02
  • Vue開(kāi)發(fā)實(shí)踐指南之應(yīng)用入口

    Vue開(kāi)發(fā)實(shí)踐指南之應(yīng)用入口

    這篇文章主要給大家介紹了關(guān)于Vue開(kāi)發(fā)實(shí)踐指南之應(yīng)用入口的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • vue動(dòng)態(tài)合并單元格并添加小計(jì)合計(jì)功能示例

    vue動(dòng)態(tài)合并單元格并添加小計(jì)合計(jì)功能示例

    這篇文章主要給大家介紹了關(guān)于vue動(dòng)態(tài)合并單元格并添加小計(jì)合計(jì)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 手把手教你用vue3開(kāi)發(fā)一個(gè)打磚塊小游戲

    手把手教你用vue3開(kāi)發(fā)一個(gè)打磚塊小游戲

    這篇文章主要給大家介紹了關(guān)于如何利用vue3開(kāi)發(fā)一個(gè)打磚塊小游戲的相關(guān)資料,通過(guò)一個(gè)小游戲?qū)嵗梢钥焖倭私鈜ue3開(kāi)發(fā)小游戲的流程,需要的朋友可以參考下
    2021-07-07
  • vue打印瀏覽器頁(yè)面功能的兩種實(shí)現(xiàn)方法

    vue打印瀏覽器頁(yè)面功能的兩種實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于vue打印瀏覽器頁(yè)面功能的兩種實(shí)現(xiàn)方法,這個(gè)功能其實(shí)也是自己學(xué)習(xí)到的,做完也有一段時(shí)間了,一直想記錄總結(jié)一下,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • Vue 3 中的 toRef 和 toRefs 函數(shù)案例講解

    Vue 3 中的 toRef 和 toRefs 函數(shù)案例講解

    這篇文章主要介紹了Vue 3 中的 toRef 和 toRefs 函數(shù),toRef 和 toRefs 函數(shù)是 Vue 3 中的兩個(gè)非常有用的函數(shù),它們可以幫助我們更好地管理組件中的響應(yīng)式數(shù)據(jù),并且可以提高組件的性能和用戶體驗(yàn),需要的朋友可以參考下
    2024-06-06
  • element UI 2.15.13與vue2.0表格勾選回顯關(guān)鍵demo

    element UI 2.15.13與vue2.0表格勾選回顯關(guān)鍵demo

    這篇文章主要為大家介紹了element UI 2.15.13與vue2.0表格勾選回顯關(guān)鍵demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • vue項(xiàng)目發(fā)布有緩存正式環(huán)境不更新的解決方案

    vue項(xiàng)目發(fā)布有緩存正式環(huán)境不更新的解決方案

    vue項(xiàng)目每次發(fā)布新版本后,測(cè)試人員都要強(qiáng)制刷新才能更新瀏覽器代碼來(lái)驗(yàn)證bug,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目發(fā)布有緩存正式環(huán)境不更新的解決方案,需要的朋友可以參考下
    2024-03-03

最新評(píng)論

宜昌市| 吴桥县| 江阴市| 拜城县| 洪雅县| 三原县| 故城县| 榆树市| 罗源县| 奇台县| 正镶白旗| 桂东县| 天峨县| 眉山市| 威远县| 额敏县| 芒康县| 锦屏县| 祁连县| 荃湾区| 镇原县| 黑水县| 烟台市| 屏南县| 垣曲县| 河间市| 尼玛县| 石门县| 海伦市| 子长县| 六枝特区| 大同县| 紫阳县| 广州市| 阿拉善右旗| 南郑县| 磐安县| SHOW| 兴义市| 金坛市| 旬邑县|