Pinia使用(无教程只有几页代码)

安装Pinia包

npm install pinia

main.js注册Pinia

import { createPinia } from "pinia";
const app = createApp(App)
const pinia = createPinia();
app.use(pinia);

在src下创建store/store.js文件,放入以下内容

import { defineStore } from "pinia";
// 导出方法(每个方法对应一个模块,相当于vuex的模块化,引入组件时按需引入)
export const xj = defineStore("main", {
  state: () => {
    return {
      name: "萧寂",
      age: 22,
      sex: "男",
    };
  },
  getters: {
    getAddAge: (state) => {
        // 传参在返回的函数写传入的参数
      return (num) => state.age + num;
    },
    // 在getter里面调用其他的getter使用this
    getNameAndAge() {
      return this.name + this.getAddAge(10); // 调用其它getter
    },
  },
  actions: {
    saveName(name) {
      //访问state里面的数据使用this.
      setTimeout(() => {
        this.name = name;
      }, 1000);
    },
  },
});

在app.vue中的使用(在其他组件也一样的)

<script setup>
//解构出store.js内的需要的方法(每个方法对应一个模块,相当于vuex的模块化)
import { xj } from "./store/store";

//将数据变成响应式的方法
import { storeToRefs } from "pinia";

// 调用解构出来的方法
const store = xj();

//将store内的属性变成响应式的
storeToRefs(store);
//也可以(二者使用方式等价)
// const {name,age} = storeToRefs(store); //此时的name和age也是响应式的,但和ref不同,修改name或者age需要用store调用,如store.name=''

//修改数据
const changeName = () => {
  store.name = "张三";
};

//还原/重置所有数据
const reasetName = () => {
  store.$reset();
};

//批量修改数据
const pathStore = () => {
  store.$patch({
    name: "小红",
    age: 50,
  });
};

//
const channame = () => {
  store.saveName("二哈");
};
</script>

<template>
  <div class="">{{ store.name }}</div>
  <div>年龄: {{ store.age }}</div>
  <!-- 访问store里面的getters方法 -->
  <p>getters返回的新年龄---{{ store.getAddAge(50) }}</p>
  <p>调用其它getter:{{ store.getNameAndAge }}</p>
  <button @click="changeName">更改姓名</button>
  <button @click="reasetName">重置/还原姓名</button>
  <button @click="pathStore">批量修改数据</button>
  <button @click="channame">调用actions的方法一秒后改变姓名</button>
</template>

<style scoped lang="scss"></style>

接下来直接运行就好

效果图

在这里插入图片描述

下面是两种写法

选项式写法

import { defineStore } from 'pinia';
// import { useUserStore } from "@/stores/user";


// 定义store实例,使用 defineStore 函数
// 参数1:是store的标识,一个项目中保持唯一
// 参数2:定义store的数据(sate、getters、actions),有两种写法:1、选项式;2、组合式

// 选项式的写法:
export const userCounterStore = defineStore('counter', {
	state: () => ({
		count: 12,
		num: 22,
		val: 2,
	}),

	getters: {
		doubleCount: state => state.count * 2,
	},

	// actions 可以包含同步操作,也能包含异步操作
	actions: {
		// 在action中定义的函数,可以使用this访问state/getters,其他actions
    increase () {
      // 访问其他的store
      // const userStore = useUserStore();

			// console.log('increase this: ', this);
			// this.count++;
			// this.setCount({ type: 0, value: userStore.userinfo.age });
			this.setCount({ type: 0, value: 1 });
		},
		setCount({ type, value }) {
			if (type === 0) {
				this.count += value;
			} else {
				this.count -= value;
			}
		},
	},
});

组合式写法

import { defineStore, storeToRefs } from 'pinia';
import { computed, ref } from 'vue';
import { userCounterStore } from '@/stores/counter';

// 组合式写法:
export const useUserStore = defineStore('user', () => {
  // 在函数中,获取其他的store
  const counterStore = userCounterStore();
  const {count} = storeToRefs(counterStore)

  // console.log('counterStore.count: ', counterStore.count);
	// 1、声明state,使用ref
	const userinfo = ref({
		name: '张三',
		age: count,
	});

	// 2、声明getters,使用computed
	const desc = computed(() => '这是' + userinfo.value.name + ',年龄是' + userinfo.value.age);

	// 3、声明actions,和setup中一样直接写函数
  const setAge = val => {
		// userinfo.value.age = counterStore.count; // 可以使用
		userinfo.value.age = count; // 使用 storeToRefs 解构的属性也能用
	};

  return {
    userinfo,
    desc,
    setAge,
  }
});

使用时

import {useUserStore} from '@/store/home.js'
const store = useUserStore()
store.(return的数据)  //这样就可以访问里面的数据和方法了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧寂173

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值