大菠萝?Pinia

Pinia是一个全新的Vue状态管理库,是Vuex的代替者,尤雨溪强势推荐

  1. Vue2 和 Vue3 都能支持
  2. 抛弃传统的 Mutation ,只有 state, getteraction ,简化状态管理库
  3. 不需要嵌套模块,符合 Vue3 的 Composition api,让代码扁平化
  4. TypeScript支持
  5. 代码简介,很好的代码自动分割

安装Pinia:npm i pinia

挂载Pinia:main.js

import { createApp } from "vue";
import { createPinia } from "pinia";

import App from "./App.vue";
import router from "./router";

const app = createApp(App);

app.use(createPinia());
app.use(router);

app.mount("#app");

创建Store:store/pinia.js <pinia>

import { defineStore } from "pinia";
             
export const useCounterStore = defineStore("counter", {
  state: () => {
    return {
      msg: "张三",
    };
  },
  getters: {},
  actions: {},
});

使用  <页面>

<template>
  <div class="">
    {{ msg }}
    <button @click="handler">点我</button>
  </div>
</template>

<script setup>
import { useCounterStore } from "../stores/pinia.js";
const store = useCounterStore();
</script>

pinia结构能拿到值但是不具备响应式 需要用:storeToRefs 

// 组件

<template>
  <div class="">
    {{ msg }}
    <button @click="handler">点我</button>
  </div>
</template>

<script setup>
import { useCounterStore } from "../stores/pinia.js";
import { storeToRefs } from "pinia";
const { msg } = storeToRefs(useCounterStore());

onMounted(() => {});
const handler = () => {
    msg = '李四'
    // 结构时加上storeToRefs页面才响应
};
</script>
<style lang="scss" scoped></style>

$patch可以通过基础数据修改方式去修改多条数据,并且会加快修改速度 

// 组件

<template>
  <div class="">
    {{ msg }}
    <button @click="handler">点我</button>
  </div>
</template>

<script setup>
import { useCounterStore } from "../stores/pinia.js";
import { storeToRefs } from "pinia";
const store = useCounterStore ()

onMounted(() => {});
const handler = () => {
   store.$patch((state)=>{
    //此处state就是pinia仓库中的state
})
    
    
};
</script>

通过actions修改——store.方法名()调用 pinia中可以使用this 

//组件

<script setup>
import { useCounterStore } from "../stores/pinia.js";
const store = useCounterStore();
const initList = {
  name: "张三",
  age: 14,
};
onMounted(() => {});
const handler = () => {

  // action pinia
  store.changeState("111");
};
</script>


// pinia.js

import { defineStore } from "pinia";

export const useCounterStore = defineStore("counter", {
  state: () => {
    return {
      msg: "张三",
    };
  },
  getters: {},
  actions: {
    changeState(it) {
    //it为传过来的111 也可传对象等
      this.msg = "李四";
    },
  },
});

Pinia 中的 getter 和 Vue 中的计算属性几乎一样,在获取 State值之前做一些逻辑处理

getter 中的值有缓存特性,如果值没有改变,多次使用也只会调用一次

也可以用 this来替换State

// pinia.js

import { defineStore } from "pinia";

export const useCounterStore = defineStore("counter", {
  state: () => {
    return {
      msg: "张三",
      age: 10,
    };
  },
  getters: {
    // 定义(num) 组件调用getState时可以传参
    getState:(state) => {
      return (num) => state.age + num;
    },
    
    getNameAddState() {
      return  this.name + this.getState; //pinia中可以使用this调用其他getters
    },
  },
  actions: {
    changeState(it) {
      this.msg = "李四";
    },
  },
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
把数据存储在Vuex中的情况与之前提到的类似: 1. 共享状态:多个组件需要访问和共享相同的数据时,可以将这些数据存储在Vuex中,以便在整个应用程序中共享和管理。 2. 跨组件通信:如果需要在不同的组件之间进行通信和共享数据,可以使用Vuex来集中管理这些数据,并通过Vuex提供的API来进行读取和更新操作。 3. 异步操作:当需要进行异步操作(例如API调用)并在多个组件中共享结果时,可以使用Vuex来管理异步操作的状态,并触发相应的行为。 至于Pinia的具体使用流程,它是一个新一代的状态管理库,专为Vue 3设计。以下是Pinia的基本使用流程: 1. 安装Pinia:通过npm或yarn安装Pinia库。 2. 创建Store:创建一个Pinia的store实例,该实例包含应用程序的状态和相关操作。可以定义多个store来管理不同的领域状态。 3. 定义状态和操作:在store中定义状态和操作方法。状态可以是任何JavaScript对象,操作方法则用于对状态进行读取和更新。 4. 注册插件:将Pinia插件注册到Vue应用程序中,以便在整个应用程序中访问Pinia的store实例。 5. 在组件中使用:在Vue组件中使用`useStore`函数来获取store实例,并通过store实例访问和更新状态。 6. 在模板中使用:可以在Vue模板中通过`$store`对象来访问store实例,并在模板中直接使用状态和操作。 总之,Pinia的使用流程类似于Vuex,但具有更好的类型推断和更好的支持Vue 3的特性。它提供了简洁的API和更好的性能,使得状态管理变得更加灵活和可扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值