安装

npm i pinia

src 下创建 store/user.ts 文件

“这里我们为了方便模块使用,文件名字使用模块名字,但其实pinia是不需要模块的,便于理解,这里就这样叫了”

  1. import { defineStore } from 'pinia';
  2. export const useUserStore = defineStore('user', {
  3. state: () => {
  4. return {};
  5. },
  6. getters: {},
  7. actions: {},
  8. });

defineStore是pinia提供的方法,接受两个参数,
参数1: id/模块名
参数2:options配置项,是object类型,分别记录 state、getters、actions

在main.ts中挂载pinia

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

createPinia是pinia中提供的用于创建pinia实例的方法

state

创建state

  1. import { defineStore } from 'pinia';
  2. export const useUserStore = defineStore('user', {
  3. state: () => {
  4. return {
  5. userInfos:{
  6. name: 'zs',
  7. age: 18,
  8. sex: 'man',
  9. };
  10. }
  11. });

页面中使用 state

storeToRefs()解构出pinia中定义的state的值,并且已经是响应式的

  1. <template>
  2. <div>
  3. <ul>
  4. <li>{{ userInfos.name }}</li>
  5. <li>{{ userInfos.age }}</li>
  6. <li>{{ userInfos.sex }}</li>
  7. </div>
  8. </template>
  9. <script setup lang="ts">
  10. import { useUserStore } from '@/store/user';
  11. import { storeToRefs } from 'pinia';
  12. const store = useUserStore();
  13. const { userInfos } = storeToRefs(store);
  14. </script>
  15. <style scoped></style>

直接修改state的值

  1. <template>
  2. <div>
  3. <ul>
  4. <li>{{ userInfos.name }}</li>
  5. <li>{{ userInfos.age }}</li>
  6. <li>{{ userInfos.sex }}</li>
  7. </ul>
  8. <button @click="changeAge">修改state: age++</button><br />
  9. </div>
  10. </template>
  11. <script setup lang="ts">
  12. import { useUserStore } from '@/store/user';
  13. import { storeToRefs } from 'pinia';
  14. const store = useUserStore();
  15. console.log(store);
  16. const { userInfos } = storeToRefs(store);
  17. // 可以直接修改state的值
  18. const changeAge = () => {
  19. userInfos.value.age++;
  20. };
  21. </script>
  22. <style scoped></style>

批量修改state的值 $path

  1. <template>
  2. <div>
  3. <ul>
  4. <li>{{ userInfos.name }}</li>
  5. <li>{{ userInfos.age }}</li>
  6. <li>{{ userInfos.sex }}</li>
  7. </ul>
  8. <button @click="changeAge">修改state: age++</button><br />
  9. <button @click="pathState">批量修改state</button><br />
  10. </div>
  11. </template>
  12. <script setup lang="ts">
  13. import { useUserStore } from '@/store/user';
  14. import { storeToRefs } from 'pinia';
  15. const store = useUserStore();
  16. console.log(store);
  17. const { userInfos } = storeToRefs(store);
  18. // 可以直接修改state的值
  19. const changeAge = () => {
  20. userInfos.value.age++;
  21. };
  22. // 批量修改
  23. const pathState = () => {
  24. store.$patch({
  25. userInfos: { name: 'ks', age: 20 },
  26. });
  27. };
  28. </script>
  29. <style scoped></style>

替换state的值

  1. <template>
  2. <div>
  3. <ul>
  4. <li>{{ userInfos.name }}</li>
  5. <li>{{ userInfos.age }}</li>
  6. <li>{{ userInfos.sex }}</li>
  7. </ul>
  8. <button @click="changeAge">修改state: age++</button><br />
  9. <button @click="pathState">批量修改state</button><br />
  10. <button @click="replaceState">替换state</button><br />
  11. </div>
  12. </template>
  13. <script setup lang="ts">
  14. import { useUserStore } from '@/store/user';
  15. import { storeToRefs } from 'pinia';
  16. const store = useUserStore();
  17. console.log(store);
  18. const { userInfos } = storeToRefs(store);
  19. // 可以直接修改state的值
  20. const changeAge = () => {
  21. userInfos.value.age++;
  22. };
  23. // 批量修改
  24. const pathState = () => {
  25. store.$patch({
  26. userInfos: { name: 'ks', age: 20 },
  27. });
  28. };
  29. // 替换State
  30. const replaceState = () => {
  31. store.$state.userInfos = { age: 20, name: 'eee', sex: 'women' };
  32. };
  33. </script>
  34. <style scoped></style>

重置state的值$reset

  1. <template>
  2. <div>
  3. <ul>
  4. <li>{{ userInfos.name }}</li>
  5. <li>{{ userInfos.age }}</li>
  6. <li>{{ userInfos.sex }}</li>
  7. </ul>
  8. <button @click="changeAge">修改state: age++</button><br />
  9. <button @click="pathState">批量修改state</button><br />
  10. <button @click="replaceState">替换state</button><br />
  11. <button @click="reset">重置state的值</button><br />
  12. </div>
  13. </template>
  14. <script setup lang="ts">
  15. import { useUserStore } from '@/store/user';
  16. import { storeToRefs } from 'pinia';
  17. const store = useUserStore();
  18. console.log(store);
  19. const { userInfos } = storeToRefs(store);
  20. // 可以直接修改state的值
  21. const changeAge = () => {
  22. userInfos.value.age++;
  23. };
  24. // 批量修改
  25. const pathState = () => {
  26. store.$patch({
  27. userInfos: { name: 'ks', age: 20 },
  28. });
  29. };
  30. // 替换State
  31. const replaceState = () => {
  32. store.$state.userInfos = { age: 20, name: 'eee', sex: 'women' };
  33. };
  34. //重置state的值
  35. const reset = () => {
  36. store.$reset();
  37. };
  38. </script>
  39. <style scoped></style>

getters

创建getters

类似于vue中的计算属性computed,同样会自动缓存
接受一个参数state,并且内部的this指向state
需要接受额外的参数,需要 return 一个函数体

  1. import { defineStore } from 'pinia';
  2. export const useUserStore = defineStore('user', {
  3. state: () => {
  4. return {
  5. userInfos: { name: 'zs', age: 18, sex: 'man' },
  6. };
  7. },
  8. getters: {
  9. // 默认接受一个 state 参数
  10. getAddAge: state => {
  11. return state.userInfos.age + 10;
  12. },
  13. // getters 需要其他参数时,return 一个函数
  14. getSubAge: state => {
  15. return (num: number) => state.userInfos.age - num;
  16. },
  17. getNameAndAge(): string {
  18. return this.userInfos.name + this.getAddAge;
  19. },
  20. },
  21. },
  22. });

页面中使用getters

store.getters定义的函数名字

  1. <template>
  2. <div>
  3. <ul>
  4. <li>{{ userInfos.name }}</li>
  5. <li>{{ userInfos.age }}</li>
  6. <li>{{ userInfos.sex }}</li>
  7. <li>新的年龄:{{ store.getAddAge }}</li>
  8. <li>getters: {{ store.getNameAndAge }}</li>
  9. <li>getters: {{ store.getSubAge(2) }}</li>
  10. </ul>
  11. <button @click="changeAge">修改state: age++</button><br />
  12. <button @click="pathState">批量修改state</button><br />
  13. <button @click="replaceState">替换state</button><br />
  14. <button @click="reset">重置state的值</button><br />
  15. </div>
  16. </template>
  17. <script setup lang="ts">
  18. import { useUserStore } from '@/store/user';
  19. import { storeToRefs } from 'pinia';
  20. const store = useUserStore();
  21. console.log(store);
  22. const { userInfos } = storeToRefs(store);
  23. // 可以直接修改state的值
  24. const changeAge = () => {
  25. userInfos.value.age++;
  26. };
  27. // 批量修改
  28. const pathState = () => {
  29. store.$patch({
  30. userInfos: { name: 'ks', age: 20 },
  31. });
  32. };
  33. // 替换State
  34. const replaceState = () => {
  35. store.$state.userInfos = { age: 20, name: 'eee', sex: 'women' };
  36. };
  37. //重置state的值
  38. const reset = () => {
  39. store.$reset();
  40. };
  41. </script>
  42. <style scoped></style>

actions

创建actions

pinia中的actions真是太简单啦,不再像vuex中actions处理异步再调用mutations修改state
pinia中的actions可以随便做,同步异步,修改state,都行,这里简单写下actions的用法

  1. import { defineStore } from 'pinia';
  2. export const useUserStore = defineStore('user', {
  3. state: () => {
  4. return {
  5. userInfos: { name: 'zs', age: 18, sex: 'man' },
  6. };
  7. },
  8. getters: {
  9. // 默认接受一个 state 参数
  10. getAddAge: state => {
  11. return state.userInfos.age + 10;
  12. },
  13. // getters 需要其他参数时,return 一个函数
  14. getSubAge: state => {
  15. return (num: number) => state.userInfos.age - num;
  16. },
  17. getNameAndAge(): string {
  18. return this.userInfos.name + this.getAddAge;
  19. },
  20. },
  21. actions: {
  22. saveName(name: string) {
  23. this.userInfos.name = name;
  24. },
  25. },
  26. });

页面中使用actions

  1. <template>
  2. <div>
  3. <ul>
  4. <li>{{ userInfos.name }}</li>
  5. <li>{{ userInfos.age }}</li>
  6. <li>{{ userInfos.sex }}</li>
  7. <li>新的年龄:{{ store.getAddAge }}</li>
  8. <li>getters: {{ store.getNameAndAge }}</li>
  9. <li>getters: {{ store.getSubAge(2) }}</li>
  10. </ul>
  11. <button @click="changeAge">修改state: age++</button><br />
  12. <button @click="pathState">批量修改state</button><br />
  13. <button @click="replaceState">替换state</button><br />
  14. <button @click="reset">重置state的值</button><br />
  15. <button @click="saveName">调用actions方法保存name</button>
  16. </div>
  17. </template>
  18. <script setup lang="ts">
  19. import { useUserStore } from '@/store/user';
  20. import { storeToRefs } from 'pinia';
  21. const store = useUserStore();
  22. console.log(store);
  23. const { userInfos } = storeToRefs(store);
  24. // 可以直接修改state的值
  25. const changeAge = () => {
  26. userInfos.value.age++;
  27. };
  28. // 批量修改
  29. const pathState = () => {
  30. store.$patch({
  31. userInfos: { name: 'ks', age: 20 },
  32. });
  33. };
  34. // 替换State
  35. const replaceState = () => {
  36. store.$state.userInfos = { age: 20, name: 'eee', sex: 'women' };
  37. };
  38. //重置state的值
  39. const reset = () => {
  40. store.$reset();
  41. };
  42. // 调用actions方法
  43. const saveName = () => {
  44. store.saveName('小猪佩奇');
  45. };
  46. </script>
  47. <style scoped></style>