赤坎手机网站建设公司,在哪里做公司网站,定制西装,做棋牌网站犯法吗1. Pinia 状态管理
什么是 Pinia#xff1f;
Pinia 是 Vue 3 官方推荐的状态管理库#xff0c;可以理解为一个全局数据仓库。
为什么需要它#xff1f; 想象一下#xff0c;你有一个购物车数据#xff0c;需要在多个页面#xff08;首页、商品页、结算页Pinia是 Vue 3 官方推荐的状态管理库可以理解为一个全局数据仓库。为什么需要它想象一下你有一个购物车数据需要在多个页面首页、商品页、结算页共享。如果用 props 一层层传递会很麻烦Pinia 就是解决这个问题的。安装 Pinianpm install pinia使用步骤第一步在 main.js 中注册 Pinia// main.js import { createApp } from vue import { createPinia } from pinia import App from ./App.vue const app createApp(App) const pinia createPinia() app.use(pinia) app.mount(#app)第二步创建 Store数据仓库// src/stores/counter.js import { defineStore } from pinia import { ref, computed } from vue // defineStore 第一个参数是 store 的唯一标识名字 export const useCounterStore defineStore(counter, () { // state数据 // 就像组件中的 ref存放响应式数据 const count ref(0) const name ref(小明) // getters计算属性 // 就像组件中的 computed const doubleCount computed(() count.value * 2) // actions方法 // 修改数据的方法 function increment() { count.value } function decrement() { count.value-- } // 异步操作也可以放在 actions 中 async function asyncIncrement() { // 模拟接口请求 await new Promise(resolve setTimeout(resolve, 1000)) count.value } // 必须 return 出去才能在组件中使用 return { count, name, doubleCount, increment, decrement, asyncIncrement } })第三步在组件中使用 Store爷爷组件 (GrandParent.vue)template div classgrandparent h2 爷爷组件/h2 p从 Pinia 获取的 count: {{ counterStore.count }}/p p双倍 count: {{ counterStore.doubleCount }}/p button clickcounterStore.increment爷爷点击 1/button !-- 嵌套父组件 -- ParentComponent / /div /template script setup import { useCounterStore } from /stores/counter import ParentComponent from ./ParentComponent.vue // 获取 store 实例直接使用即可 const counterStore useCounterStore() /script父组件 (ParentComponent.vue)template div classparent h3 父组件/h3 p从 Pinia 获取的 count: {{ counterStore.count }}/p button clickcounterStore.decrement父亲点击 -1/button !-- 嵌套子组件 -- ChildComponent / /div /template script setup import { useCounterStore } from /stores/counter import ChildComponent from ./ChildComponent.vue // 任何组件都可以直接获取同一个 store const counterStore useCounterStore() /script孙子组件 (ChildComponent.vue)template div classchild h4 孙子组件/h4 p从 Pinia 获取的 count: {{ counterStore.count }}/p button clickcounterStore.asyncIncrement孙子异步 11秒后/button /div /template script setup import { useCounterStore } from /stores/counter // 三层组件都能访问同一份数据 const counterStore useCounterStore() /scriptPinia 小结概念说明Store全局数据仓库所有组件都能访问State存放数据的地方用 ref 或 reactiveGetters计算属性用 computedActions修改数据的方法普通函数可以是异步的2. Provide / Inject 依赖注入什么是 Provide / Inject这是 Vue 内置的跨层级传值方案不需要安装额外的库。provide提供祖先组件提供数据inject注入后代组件接收数据与 Pinia 的区别Provide/Inject 的数据是局部的只在当前组件树内共享而 Pinia 是全局的。基本使用爷爷组件提供数据template div classgrandparent h2 爷爷组件 - Provide 示例/h2 p爷爷的消息: {{ message }}/p p爷爷的主题色: {{ theme }}/p button clickchangeTheme切换主题/button ParentProvide / /div /template script setup import { ref, provide } from vue import ParentProvide from ./ParentProvide.vue // 定义要共享的数据 const message ref(来自爷爷的问候~) const theme ref(light) // 切换主题的方法 const changeTheme () { theme.value theme.value light ? dark : light } // 使用 provide 提供数据 // provide(key, value) // key: 字符串或 Symbol用于标识 // value: 要共享的数据 // 提供普通数据 provide(message, message) // 提供响应式数据子组件能感知变化 provide(theme, theme) // 提供方法让子组件也能修改数据 provide(changeTheme, changeTheme) // 提供对象包含多个相关数据 provide(userInfo, { name: 张三, age: 60, role: grandparent }) /script父组件中间层不需要处理template div classparent h3 父组件/h3 p我是中间层不需要 inject数据可以跳过我直接传给孙子/p ChildInject / /div /template script setup import ChildInject from ./ChildInject.vue // 注意父组件不需要做任何事数据会穿透过去 /script孙子组件注入数据template div classchild :classtheme h4 孙子组件 - Inject 示例/h4 !-- 使用注入的数据 -- p收到爷爷的消息: {{ message }}/p p当前主题: {{ theme }}/p p用户信息: {{ userInfo.name }}, {{ userInfo.age }}岁/p !-- 调用爷爷提供的方法 -- button clickchangeTheme孙子也能切换主题/button /div /template script setup import { inject } from vue // 使用 inject 接收数据 // inject(key) 或 inject(key, 默认值) // 接收响应式数据 const message inject(message) const theme inject(theme) // 接收方法 const changeTheme inject(changeTheme) // 接收对象 const userInfo inject(userInfo) // 设置默认值如果祖先没有 provide就用默认值 const notExist inject(notExist, 这是默认值) /script style scoped .light { background: #fff; color: #333; } .dark { background: #333; color: #fff; } /styleProvide / Inject 小结特点说明适用场景跨多层组件传值避免 props 层层传递响应式如果 provide 的是 ref/reactive子组件能响应变化作用范围只在当前组件树内有效与 Pinia 对比更轻量但没有 Pinia 的状态管理能力