Vue3中怎么引入Pinia存储库并使用

其他教程   发布日期:2024年04月20日   浏览次数:415

本篇内容主要讲解“Vue3中怎么引入Pinia存储库并使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue3中怎么引入Pinia存储库并使用”吧!

1.用自己最喜欢的方式安装

  1. yarn add pinia
  2. # 或者使用 npm
  3. npm install pinia

2.main.js引入

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. const app=createApp(App)
  4. import { createPinia } from 'pinia' //引入pinia
  5. app.use(createPinia())
  6. app.mount('#app')

3.创建store文件并配置内部的index.js文件

  1. import { defineStore } from 'pinia' //引入pinia
  2. //这里官网是单独导出 是可以写成默认导出的 官方的解释为大家一起约定仓库用use打头的单词 固定统一小仓库的名字不易混乱
  3. export const useCar=defineStore("test",{
  4. state: () =>{
  5. return ({
  6. msg:"这是pinia",
  7. name:"小小",
  8. age:18
  9. }) //为了避免出错,返回的值用()包起来
  10. }
  11. })

4.组件使用方法

  1. <template>
  2. <h2>{{store.msg}}{{store.name}}{{store.age}}</h2>
  3. <button @click="modify">修改store.name</button>
  4. </template>
  5. <script>
  6. import { defineComponent, ref } from 'vue';
  7. import {
  8. reactive,
  9. toRefs,
  10. onMounted,
  11. onActivated
  12. } from "vue";
  13. import {useCar} from "../store/index.js" //将之前配置的pinia文件夹中的index.js文件引入
  14. export default defineComponent({
  15. let store=useCar() //接收
  16. setup() {
  17. const state = reactive({
  18. testMsg: "原始值",
  19. });
  20. onMounted(async () => {});
  21. onActivated(() => {})
  22. const methods = {
  23. modify(){
  24. store.name = state.testMsg //修改pinia里面的数据
  25. console.log(store.name)
  26. }
  27. };
  28. return {
  29. ...toRefs(state),
  30. ...methods,
  31. };
  32. }
  33. })
  34. </script>

5.重置store.$reset()

  1. <template>
  2. <h2>{{store.msg}}{{store.name}}{{store.age}}</h2>
  3. <button @click="reset">重置store.name</button>
  4. </template>
  5. <script>
  6. import { defineComponent, ref } from 'vue';
  7. import {
  8. reactive,
  9. toRefs,
  10. onMounted,
  11. onActivated
  12. } from "vue";
  13. import {useCar} from "../store/index.js" //将之前配置的pinia文件夹中的index.js文件引入
  14. export default defineComponent({
  15. let store=useCar() //接收
  16. setup() {
  17. const state = reactive({
  18. testMsg: "原始值",
  19. });
  20. onMounted(async () => {});
  21. onActivated(() => {})
  22. const methods = {
  23. reset(){
  24. store.$reset() //重置pinia里面的数据
  25. console.log(store.name)
  26. }
  27. };
  28. return {
  29. ...toRefs(state),
  30. ...methods,
  31. };
  32. }
  33. })
  34. </script>

6.群体修改store.$patch,可以将pinia的数据进行同一修改

特点:批量修改但状态只刷新一次

  1. <template>
  2. <h2>{{store.msg}}{{store.name}}{{store.age}}</h2>
  3. <button @click="modify">修改store.name</button>
  4. <button @click="reset">重置store.name</button>
  5. <button @click="allModify">群体修改store.name</button>
  6. </template>
  7. <script>
  8. import { defineComponent, ref } from 'vue';
  9. import {
  10. reactive,
  11. toRefs,
  12. onMounted,
  13. onActivated
  14. } from "vue";
  15. import {useCar} from "../store/index.js" //将之前配置的pinia文件夹中的index.js文件引入
  16. export default defineComponent({
  17. let store=useCar() //接收
  18. setup() {
  19. const state = reactive({
  20. testMsg: "原始值",
  21. });
  22. onMounted(async () => {});
  23. onActivated(() => {})
  24. const methods = {
  25. modify(){
  26. store.name = state.testMsg //修改pinia里面的数据
  27. console.log(store.name)
  28. },
  29. reset(){
  30. store.$reset() //重置pinia里面的数据
  31. console.log(store.name)
  32. },
  33. allModify(){
  34. store.$patch({
  35. name:"花花",
  36. age:20,
  37. })
  38. }
  39. };
  40. return {
  41. ...toRefs(state),
  42. ...methods,
  43. };
  44. }
  45. })
  46. </script>

7.订阅修改

  1. //可以通过 store 的 $subscribe() 方法查看状态及其变化,通过patch修改状态时就会触发一次
  2. store.$subscribe((mutation, state) => {
  3. // 每当它发生变化时,将整个状态持久化到本地存储
  4. localStorage.setItem('hello', JSON.stringify(state))
  5. })

8.Getter

Getter 完全等同于 Store 状态的 计算值。 它们可以用 defineStore() 中的 getters 属性定义。 他们接收“状态”作为第一个参数以鼓励箭头函数的使用:(ps:虽然鼓励但是依然提供了非es6玩家的使用方式 内部可以用this代表state)

  1. //store/index.js文件
  2. export const useStore = defineStore('main', {
  3. state: () => ({
  4. counter: 0,
  5. }),
  6. getters: {
  7. doubleCount: (state) => state.counter * 2,
  8. },
  9. })
  10. //组件中直接使用: <p>Double count is {{ store.doubleCount }}</p>

9.Actions

在pinia中没有提供mutaion 因为Actions就够了(它可以异步同步的统一修改状态)之所以提供这个功能 就是为了项目中的公共修改状态的业务统一

  1. export const useStore = defineStore('main', {
  2. state: () => ({
  3. counter: 0,
  4. }),
  5. actions: {
  6. increment() {
  7. this.counter++//1.有this
  8. },
  9. randomizeCounter(state) {//2.有参数 想用哪个用哪个
  10. state.counter = Math.round(100 * Math.random())
  11. },
  12. randomizeCounter(state) {
  13. //异步函数.接口成功赋值
  14. ajax.hplBaseApi("app/productSelection/categoryRows", {}, "post").then((res) => {
  15. state.counter = res.data.length
  16. });
  17. }
  18. },
  19. })
  20. //组件中的使用:
  21. setup() {
  22. const store = useStore()
  23. store.increment()
  24. store.randomizeCounter()
  25. }

以上就是Vue3中怎么引入Pinia存储库并使用的详细内容,更多关于Vue3中怎么引入Pinia存储库并使用的资料请关注九品源码其它相关文章!