Vue3中使用Pinia

  1. Pinia官网地址: https://pinia.web3doc.top/

  2. Pinia简介:

    Pinia 是 Vue 的存储库,它允许您跨组件/页面共享状态。

  3. 为啥不用Vuex而用Pinia:

    1、Pinia 性能优于 Vuex。
    2、Pinia 完美支持 TypeScript,Vuex 在这方面做得不是很好。
    3、Pinia 对开发工具支持很好
    4、Pinia 对调试工具(vue-devtools)也支持得很好。
    5、不需要再使用名称空间来控制 store,也不需要再考虑 store 的嵌套问题。
    6、Pinia 没有mutations,相应的工作都在actions中完成,而且actions直接支持异步函数。

  4. Pinia的安装:

    npm install pinia -D

    yarn add pinia -D

  5. 在入口文件中将pinia挂载到Vue上面


import App from "./App.vue";
import { createPinia } from "pinia";

createApp(App).use(createPinia()).mount("#app");

  1. src\store目录下面创建useXxxStore.ts文件

import { defineStore } from 'pinia'

// useXxxStore 可以是 useUserStore、useCartStore 之类的任何东西,这个名称随意
// 第一个参数是应用程序中 store 的唯一 id,切记不要与其他的重复
// 这是Option Stores写法:defineStore(name,object)
export const useXxxStore = defineStore('xxx', {
  state: () => ({ count: 0, name: 'Eduardo' }),
  getters: {
    doubleCount: (state) => state.count * 2,
  },
  actions: {
    increment() {
      this.count++
    },
  },
})
// 这是Setup Stores写法:defineStore(name,function)
export const useXxxStore = defineStore('xxx', {
  const count = ref(0)
  const name = ref('Eduardo')
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }

  return { count, name, doubleCount, increment }
})
/*
在这个文件中,我们通过 export 暴露了一个 useXxxStore 方法,
这个方法是通过 Pinia 的defineStore方法创建的,
在 Vue 业务组件中执行这个函数实例才会得到真正的 Store。
*/
  1. 在其他文件中使用pinia

import { useStore } from '@/store/useXxxStore'
// 在setup()中使用
export default {
  setup() {
    const store = useStore()
	// const { name, doubleCount } = store
	//这不起作用,因为它会破坏响应式
    return {
      // 您可以返回整个 store 实例以在模板中使用它
      store,
      // 这将是响应式的
      doubleValue: computed(() => store.doubleCount),
    }
  },
}
// 在setup语法糖中使用
let store = useChatStore();
const doubleValue = computed(() => store.doubleCount);
onMounted(()=>{
	store.increment()
	//在当前组件渲染完成后,我们调用了store对象的increment方法,进行了count++操作
	// 实际上我们在组件渲染完成时,还可以通过如下代码来count++:
	//store.$patch((v) => (v.count++));
})
/*
store 对象是通过useXxxStore方法获取的,
useXxxStore方法就是我们前面介绍的useXxxStore.ts导出的方法。
得到 store 对象之后,可以直接使用store获取 Store 对象里的数据。
*/

注意:store不能使用解构语法进行操作,因为这会破坏响应式

在模板中使用store

<template>
  <div class="ChatList">
    <div class="ListBox">
      {{store.name }}:{{store.count }}
    </div>
    <div class="ListBox">
      doubleValue :{{store.doubleValue }}
    </div>
  </div>
</template>
  1. 如何订阅 Store
import { useChatStore } from "@/store/useXxxStore";
let store = useXxxStore();
//订阅Store内数据的变化
store.$subscribe((mutations, state) => {
  console.log("count",state.count)
});
/*
在上面代码中我们使用store对象的$subscribe方法订阅了数据变更事件,
无论什么时候 store 内的数据发生了变化,都会执行我们为$subscribe方法提供的回调函数。

无论是通过increment()方法更新数据,还是通过$patch方法更新数据,都会触发订阅事件。


*/

订阅回调函数有两个参数 ,第一个是 mutations 参数,这个参数的events属性携带着变更前的值和变更后的值,但这个属性只有在开发环境下存在,生产环境下不存在。订阅的第二个参数是 state,这个参数包含 store 中的数据。

以这种方式更新 store 里的数据,不利于复用数据更新的逻辑,接下来我就介绍可以复用数据更新逻辑的方案。

  1. 如何进行store 的互访
    src\store目录下面创建useYyyStore.ts文件
import { defineStore } from 'pinia'
export const useYyyStore = defineStore('yyy', {
  const count2 = ref(0)
  let initData = (chat) => {
    let result = chat.name + chat.count;
    data.value = result;
  };
  return { data, initData };
})

我们完全可以在increment方法内使用useYyyStore提供的方法。

import { useYyyStore} from "./useYyyStore";
export const useXxxStore= defineStore("xxx", () => {
  const count = ref(0)
  const name = ref('Eduardo')
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++;
    let yyyStore= useYyyStore(); //新增的行
    yyyStore.initData({name,count}); //新增的行
  }

  return { count, name, doubleCount, increment }
});
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue 3 可以使用 PWA 相关库 Pinia 来管理状态。 首先,需要在 Vue 3 安装 Pinia: ``` npm install @vueuse/pinia ``` 然后,在项目使用 Pinia,可以在 main.js 进行配置: ``` import { createApp } from 'vue' import App from './App.vue' import { createPinia } from '@vueuse/pinia' const pinia = createPinia() const app = createApp(App) app.use(pinia) app.mount('#app') ``` 在组件使用 Pinia,可以用 `setup()` 函数进行配置: ``` import { useStore } from '@vueuse/pinia' export default { setup() { const store = useStore('example') return { count: store.state.count, increment() { store.commit('increment') } } } } ``` 然后就可以在组件使用 `count` 和 `increment()` 了。 ### 回答2: Pinia 是 Vue 3 生态系统的状态管理库,它是一个为 Vue 3 设计的简单但功能强大的状态管理解决方案。使用 Pinia 可以更好地管理和组织 Vue 3 应用程序的状态。 在 Vue 3 使用 Pinia 非常简单。首先,我们需要安装 Pinia: npm install pinia 然后,在我们的应用程序的入口文件导入并创建一个 Pinia 实例: import { createPinia } from 'pinia' import { createApp } from 'vue' const pinia = createPinia() const app = createApp(App) app.use(pinia) app.mount('#app') 现在,我们可以在我们的组件使用状态管理了。我们可以使用 defineStore 函数来定义一个存储,该存储将包含我们的状态和一些操作。例如,我们可以定义一个名为 "counter" 的存储: import { defineStore } from 'pinia' export const useCounterStore = defineStore('counter', { state: () => ({ count: 0 }), actions: { increment() { this.count++ }, decrement() { this.count-- } } }) 然后,在我们的组件使用该存储: <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> <button @click="decrement">Decrement</button> </div> </template> <script> import { useCounterStore } from './store' export default { setup() { const counter = useCounterStore() return { count: counter.count, increment: counter.increment, decrement: counter.decrement } } } </script> 现在,我们可以在组件使用 count 变量来访问存储的计数,并通过点击按钮来增加或减少计数。 总结起来,Vue 3 使用 Pinia 只需几步即可轻松实现状态管理。首先,我们需要安装 Pinia 并在入口文件创建 Pinia 实例。然后,我们使用 defineStore 函数定义我们的存储,并在组件使用该存储。这使得我们可以使用存储的状态和操作来管理和共享应用程序的状态。 ### 回答3: Vue 3是一种用于构建用户界面的现代JavaScript框架,而Pinia是使用Vue 3生态系统的新状态管理库。 Vue 3使用Pinia主要可以提供更好的状态管理和数据流解决方案。 Pinia的主要特点之一是它是基于Vue 3的新响应式系统创建的。与传统的Vue 2响应式系统相比,它具有更高的性能和更好的内存管理。此外,Pinia还提供了更好的组织代码的结构,使项目更加可维护和可扩展。 在Vue 3使用Pinia的第一步是安装Pinia插件。可以通过使用npm或yarn命令来安装它。安装完成后,需要在应用程序的入口文件注册Pinia插件。 接下来,可以在Vue组件使用Pinia状态。首先,需要导入createPinia函数并使用它创建一个Pinia实例。然后,可以使用该实例的`useStore`函数来创建和使用Pinia存储。 Pinia存储是一个类,它通过定义状态和方法来跟踪和管理应用程序的数据。可以在存储类定义一些公共状态、计算属性和方法。存储类可以在Vue组件实例化,并通过Vue组件的provide/inject机制进行共享。 另外,在Vue 3,可以使用defineProps和defineEmits函数来定义组件的输入和输出。这些函数使得组件的属性和事件变得类型安全和更易于维护。 总之,Vue 3和Pinia之间的结合为Vue开发者提供了更好的状态管理和数据流解决方案。通过使用Pinia,可以更好地组织代码并提高应用程序的性能和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值