目录
一、概念
1. Pinia => Pinia
2. Pinia和Vuex的对比
01 - 不是已经有Vuex了吗?为什么还要用Pinia
02 - 和Vuex相比,Pinia有很多的优势
二、使用Pinia
1. 安装
2. 创建Pinia
3. 在main.js中引入
三、Store
1. 概念
2. 创建
3. 使用
4. 效果
四、核心概念State
1. 定义State
2. 读取写入State
3. 重置State
4. 改变State
5. 替换State
五、核心概念Getters
1. 基本使用
代码
使用
2. 在getter中使用其他的getter
3. getters支持返回一个函数
代码
使用
4. getters使用别的store中的数据
六、核心概念Actions
1. 基本使用
代码
使用
2. 异步操作
代码
使用
Pinia(发音为/piːnjʌ/,如英语中的“peenya”)是最接近piña(西班牙语中的菠萝)的词
npm install pinia
创建stores文件夹,并在其中创建个index.js
// 1. 导入
import { createPinia } from 'pinia';
// 2. 创建
const pinia = createPinia();
// 3. 导出
export default pinia;
import { createApp } from 'vue';
import App from './App.vue';
// 1. 导入
import pinia from './stores';
// 2. use一下
createApp(App).use(pinia).mount('#app');
Store :
Store有三个核心概念 :
定义一个Store :
在stores文件夹创建 counter.js 文件
// 1, 导入
import { defineStore } from 'pinia';
// 2. 创建一个store
/**
* 第一个参数 : 唯一的名称
* 第二个参数 : 传入配置
* 返回值 : 返回一个函数,调用这个函数,即可拿到当前store
*/
const userCounterStore = defineStore('counterStore', {
state: () => ({
count: 66
})
});
export default userCounterStore;
App 页面
1. counterStore : {{ counterStore.count }}
2. toRefs : {{ aCount }}
3. storeToRefs : {{ bCount }}
// 1, 导入
import { defineStore } from 'pinia';
// 2. 创建一个store
/**
* 第一个参数 : 唯一的名称
* 第二个参数 : 传入配置
* 返回值 : 返回一个函数,调用这个函数,即可拿到当前store
*/
const userCounterStore = defineStore('counterStore', {
state: () => ({
count: 66,
name: 'coder',
age: 19
})
});
export default userCounterStore;
默认情况下,可以通过 store 实例访问状态来直接读取和写入状态
可以通过调用 store 上的 $reset() 方法将状态 重置 到其初始值
// 重置
const resetState = () => {
// 回到初始值
counterStore.$reset();
};
除了直接用 store.counter++ 修改 store,还可以调用 $patch 方法
允许同时应用多个更改
// 监听点击
const changCount = () => {
// 一起更改数据
counterStore.$patch({
count: 99,
name: 'star',
// 如果输入新增的属性,没有用哒!
buy: ref('abvc')
});
console.log(counterStore);
};
可以通过将其 $state 属性设置为新对象来替换 Store 的整个状态
const userCounterStore = defineStore('counterStore', {
state: () => ({
count: 66,
name: 'coder',
age: 19
}),
getters: {
// 1. 定义getterts
doubleCount(state) {
// 2. 通过state参数拿到count
console.log(state.count);
// 3. 通过this拿到参数
console.log(this.count);
}
}
});
home
count : {{ counterStore.count }}
count : {{ counterStore.doubleCount }}
getters: {
doubleCount(state) {
return this.count * 2;
},
othersGetter() {
// 通过this来拿
return this.doubleCount;
}
}
可以用来传递参数到getters
getters: {
doubleCount(state) {
return this.count * 2;
},
formatName() {
// 返回一个函数,可以传递参数进来
return (lastName) => {
return this.name + lastName;
};
}
}
{{ counterStore.formatName('123') }}
导入其他的store,使用即可,很方便
userOtherStore(){
// 1. 导入其他soter
const otherStore = userOtherStore()
// 2. 拿到数据 ....
otherStore.getters()
}
actions => 非常适合定义业务逻辑
const userCounterStore = defineStore('counterStore', {
state: () => ({
count: 66,
name: 'coder',
age: 19
}),
actions: {
increment() {
this.count++;
},
// 这里的参数指调用时传递过来的参数
incrementNum(num) {
this.count += num;
}
}
});
const userCounterStore = defineStore('counterStore', {
state: () => ({
arrList: []
}),
actions: {
async fetchDataList() {
// 1. 请求
const res = await fetch('http:xxxxx');
const data = await res.json();
this.arrList = data.list;
// 2. 返回值,相当于 return Promise.resolve(data)
return data;
}
}
});