中国建设银行北京分行门户网站公告,淘宝代运营一般多少钱,做视频网站需要多大带宽,购买手机网站推荐Vue 项目中的状态优化一般都会用Pinia替代Vuex#xff0c;Pinia 是 Vue 生态系统中的一个轻量级状态管理库#xff0c;作为 Vuex 的替代品#xff0c;它提供了更简洁的 API 和更好的性能。模块化管理#xff1a;使用 Pinia 时#xff0c;建议将状态拆分为多个 store 模块Pinia 是 Vue 生态系统中的一个轻量级状态管理库作为 Vuex 的替代品它提供了更简洁的 API 和更好的性能。模块化管理使用 Pinia 时建议将状态拆分为多个 store 模块以避免单一状态树过于庞大和复杂。这不仅有助于维护还能提升性能。懒加载 Store通过 Pinia 的 defineStore 动态创建 store当某个 store 仅在特定页面或组件中需要时可以延迟加载它。这样可以减少应用的初始加载时间。State 持久化如果某些状态需要在页面刷新后保持可以使用 Pinia 的插件功能将状态持久化到 localStorage 或 sessionStorage避免不必要的网络请求或重新计算。避免不必要的深度响应Pinia 允许你明确哪些状态需要响应式哪些不需要。对于不需要响应式的复杂对象可以使用 shallowRef 或 shallowReactive 来减少响应式开销。1. 安装 Pinia 与配置
npm install pinia设置 Pinia
import { createApp } from vue
import { createPinia } from pinia
import App from ./App.vueconst app createApp(App)// 创建 Pinia 实例
const pinia createPinia()app.use(pinia)
app.mount(#app) 2. 创建模块化 Store创建两个 Store 模块userStore 和 productStore。其中 userStore 将使用状态持久化productStore 将演示懒加载和避免不必要的深度响应。stores/userStore.js
// stores/userStore.js
import { defineStore } from pinia
import { ref, computed, watch } from vueexport const useUserStore defineStore(user, () {// 初始化状态如果 localStorage 中有存储优先使用存储的状态const name ref(localStorage.getItem(user-name) || John Doe)const age ref(parseInt(localStorage.getItem(user-age)) || 25)const doubleAge computed(() age.value * 2)// 监听状态变化并将其保存到 LocalStoragewatch(() name.value,(newValue) {localStorage.setItem(user-name, newValue)})watch(() age.value,(newValue) {localStorage.setItem(user-age, newValue.toString())})return {name,age,doubleAge,}
})stores/productStore.js
// stores/productStore.js
import { defineStore } from pinia
import { shallowRef } from vueexport const useProductStore defineStore(product, () {// 使用 shallowRef 来避免不必要的深度响应const products shallowRef([])const addProduct (product) {products.value.push(product)}return {products,addProduct,}
})3. 懒加载 StoreproductStore 仅在需要时加载例如在某个特定组件中。components/ProductList.vue
templatedivh2Product List/h2ulli v-forproduct in products :keyproduct.id{{ product.name }}/li/ulbutton clickaddNewProductAdd Product/button/div
/templatescript setup
import { useProductStore } from stores/productStore
import { onMounted } from vue// 懒加载 productStore
const productStore useProductStore()
const products productStore.productsconst addNewProduct () {productStore.addProduct({ id: Date.now(), name: Product ${products.length 1} })
}onMounted(() {console.log(ProductList component mounted.)
})
/script4. 在其他组件中使用 userStorecomponents/UserProfile.vue
templatedivh2User Profile/h2pName: {{ name }}/ppAge: {{ age }}/ppDouble Age: {{ doubleAge }}/p/div
/templatescript setup
import { useUserStore } from /store/userStore// 使用 userStore这个 Store 状态会被持久化
const userStore useUserStore()const { name, age, doubleAge } userStore
/script5. 手动实现状态持久化我们在 userStore 中通过 localStorage 手动实现了状态持久化。如果你需要更加通用的状态持久化插件可以创建一个简单的 Pinia 插件。plugins/persistedState.js
// plugins/persistedState.js
export function createPersistedStatePlugin(options {}) {return ({ store }) {const { key store.$id } options// 从 LocalStorage 初始化状态const fromStorage localStorage.getItem(key)if (fromStorage) {store.$patch(JSON.parse(fromStorage))}// 订阅状态变化并将其保存到 LocalStoragestore.$subscribe((mutation, state) {localStorage.setItem(key, JSON.stringify(state))})}
}注册插件
import { createPinia } from pinia
import { createPersistedStatePlugin } from ./plugins/persistedStateconst pinia createPinia()
pinia.use(createPersistedStatePlugin())const app createApp(App)
app.use(pinia)
app.mount(#app)