Vue pinia模塊化全局注冊詳解
Vue3中對pinia模塊化全局注冊
項目小使用簡易的方式還不覺得,但是項目大了后就會發(fā)現(xiàn)引入的東西有些重復(fù)了
安裝
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);
相對來說項目小的話沒什么必要做全局,但是項目大了可能這樣會好維護(hù)一些
當(dāng)然也會有更好的方式,只是我沒發(fā)現(xiàn)
最后補充
打包解耦
到這里還不行,為了讓appStore實例與項目解耦,在構(gòu)建時要把appStore抽取到公共chunk,在vite.config.ts做如下配置
build: {
outDir: "dist",
rollupOptions: {
output: {
manualChunks(id) {
//靜態(tài)資源分拆打包
...其他配置
// 將pinia的全局庫實例打包進(jìn)vendor,避免和頁面一起打包造成資源重復(fù)引入
if (id.includes(resolve(__dirname, "/src/store/index.ts"))) {
return "vendor";
}
},
},
},
}
到此這篇關(guān)于Vue pinia模塊化全局注冊詳解的文章就介紹到這了,更多相關(guān)Vue pinia模塊化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用vue2實現(xiàn)帶地區(qū)編號和名稱的省市縣三級聯(lián)動效果
我們知道省市區(qū)縣都有名稱和對應(yīng)的數(shù)字唯一編號,使用編號可以更方便查詢以及程序處理,我們今天來了解一下使用vue2來實現(xiàn)常見的省市區(qū)下拉聯(lián)動選擇效果,需要的朋友可以參考下2018-11-11
探秘Vue異步更新機(jī)制中nextTick的原理與實現(xiàn)
nextTick?是?Vue?提供的一個重要工具,它的作用主要體現(xiàn)在幫助我們更好地處理異步操作,下面就跟隨小編一起來探索一下nextTick的原理與實現(xiàn)吧2024-02-02
Vue 3 中的 toRef 和 toRefs 函數(shù)案例講解
這篇文章主要介紹了Vue 3 中的 toRef 和 toRefs 函數(shù),toRef 和 toRefs 函數(shù)是 Vue 3 中的兩個非常有用的函數(shù),它們可以幫助我們更好地管理組件中的響應(yīng)式數(shù)據(jù),并且可以提高組件的性能和用戶體驗,需要的朋友可以參考下2024-06-06
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項目發(fā)布有緩存正式環(huán)境不更新的解決方案
vue項目每次發(fā)布新版本后,測試人員都要強制刷新才能更新瀏覽器代碼來驗證bug,下面這篇文章主要給大家介紹了關(guān)于vue項目發(fā)布有緩存正式環(huán)境不更新的解決方案,需要的朋友可以參考下2024-03-03

