Vue pinia模塊化全局注冊詳解
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項目部署到nginx/tomcat服務器的實現(xiàn)
這篇文章主要介紹了vue項目部署到nginx/tomcat服務器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08vue使用this.$message不生效的部分原因及解決方案
這篇文章主要介紹了vue使用this.$message不生效的部分原因及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09