在vue3中使用pinia 1.pinia基础用法 2.修改state中数据的多种方法 3.$subscribe 监听state中值的变化

1.准备

  1. 使用vite创建好一个vue3项目,开发语言选择ts
  2. 使用 npm i pinia -s 安装最新版本的pinia 这里我的版本安装的是 2.1.4

2.注册pinia

1.在main中注册pinia

import { createApp, createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import { createPinia } from "pinia";

let app = createApp(App);
const store = createPinia();

app.use(store);
app.mount("#app");

2.在store中创建index.ts和store-name.ts文件

index.ts内容如下:

import { defineStore } from "pinia";

import { Names } from "./store-name";
export const useTestStore = defineStore(Names.TEST, {
  state: () => {
    return {
      current: 1,
      name: "不染-9732",
    };
  },
  getters: {},
  actions: {},
});

store-name.ts内容如下:

export const enum Names {
  TEST = "TEST",
}

文件格式

3.在app.vue文件中使用store中的参数

app.vue文件的内容如下:

<template>
  <div>app.vue===>pinia:{{ Test.current }}--{{ Test.name }}</div>
</template>

<script setup lang="ts">
import { useTestStore } from "./store/index";
const Test = useTestStore();
</script>

页面输出如下内容则一次简单的pinia的调用完成

输出结果

4.修改store中参数的值

1.直接修改 Test.current++

<template>
  <div>app.vue===>pinia:{{ Test.current }}--{{ Test.name }}</div>
  <button @click="change">修改值</button>
</template>

<script setup lang="ts">
import { useTestStore } from "./store/index";
const Test = useTestStore();

const change=()=>{
  Test.current++
}
</script>

另存为

2.使用$patch批量修改


const change=()=>{
  // Test.current++
  Test.$patch({
    current:9999,
    name:'一刀!'
  })
}

一刀

3.使用$patch结合工厂函数修改内容【推荐】


const change=()=>{
  Test.$patch((state=>{
    state.current=999999,
    state.name='两刀'
  }))
}

4.使用$state完全覆盖原有对象进行修改【需要完全覆盖 不推荐】

const change=()=>{
  Test.$state={
    current:66,
    name:'非酋玩家'
  }
}

5.使用store中的actions定义方法修改state中的值

修改store中的index.ts中内容如下

import { defineStore } from "pinia";

import { Names } from "./store-name";
export const useTestStore = defineStore(Names.TEST, {
  state: () => {
    return {
      current: 1,
      name: "不染-9732",
    };
  },
  getters: {},
  // methods  能够做同步,异步   提交state
  actions: {
    setCurrent(num:number) {
      this.current = num;
    },
    setName(name:string) {
        this.name = '土豪玩家';
      },
  },
});

完整的App.vue的内容如下:

<template>
  <div>app.vue===>pinia:{{ Test.current }}--{{ Test.name }}</div>
  <button @click="changeCurrent">修改值</button
  ><button @click="changeName">修改名字</button>
</template>

<script setup lang="ts">
import { useTestStore } from "./store/index";
const Test = useTestStore();

const changeCurrent = () => {
  // Test.current++
  // Test.$patch({
  //   current:9999,
  //   name:'一刀!'
  // })
  // Test.$patch((state=>{
  //   state.current=999999,
  //   state.name='两刀'
  // }))
  // Test.$state={
  //   current:66,
  //   name:'非酋玩家'
  // }
  Test.setCurrent(888888);
};

const changeName = () => {
  Test.setName("土豪玩家");
};
</script>

土豪

5. pinia中的$subscribe 监听state中值的变化

<template>
  <div>app.vue===>数值:{{ Test.current }}  --名字:{{ Test.name }}</div>
  <button @click="changeCurrent">修改值</button
  ><button @click="changeName">修改名字</button>
</template>

<script setup lang="ts">
import { useTestStore } from "./store/index";
const Test = useTestStore();

Test.$subscribe((args,state)=>{
  // args  当前修改的数据
  // state  state中所有数据的值
  console.log('args===>',args);
  console.log('state===>',state);
  
})

const changeCurrent = () => {
  Test.current++
};

const changeName = () => {
  Test.setName("土豪玩家");
};
</script>

监听

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值