项目中安装pinia
| yarn add pinia |
| |
| npm install pinia |
复制
| |
| import { createApp } from 'vue'; |
| import App from './App.vue'; |
| const app = createApp(App); |
| |
| |
| import { createPinia } from "pinia" |
| app.use(createPinia()); |
复制
定义store,渲染state
在 src 目录下新建 store 目录,store 目录下新建 demo.ts
| import { defineStore } from 'pinia'; |
| |
| export const demoStore = defineStore('demo', { |
| state: () => { |
| return { |
| |
| counter: 0 as Number, |
| mobile:"13111111111" as String |
| } |
| }, |
| getters:{}, |
| actions:{} |
| }) |
复制
在页面组件中渲染 state
| <script lang="ts" setup> |
| |
| import { storeToRefs } from "pinia"; |
| |
| import { demoStore } from '@/store/demo'; |
| |
| |
| const store = demoStore(); |
| |
| const { counter } = storeToRefs(store); |
| </script> |
| |
| <template> |
| <div> |
| pinia测试{{counter}} |
| </div> |
| </template> |
| |
| <style lang="less" scoped> |
| |
| </style> |
复制
修改state的几种方式
| |
| |
| <script lang="ts" setup> |
| |
| import { storeToRefs } from "pinia"; |
| |
| import { demoStore } from '@/store/demo'; |
| |
| |
| const store = demoStore(); |
| |
| const { counter } = storeToRefs(store); |
| |
| |
| |
| |
| |
| |
| const addCounter = () => { |
| store.$patch({ |
| count:store.counter += 2, |
| mobile:"13122222222" |
| }) |
| store.getList(); |
| }; |
| const reduceCounter = () => { |
| store.$patch({ |
| count:store.counter -= 2, |
| mobile:"13122222222" |
| }) |
| }; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| </script> |
| |
| <template> |
| <div> |
| pinia测试{{counter}} |
| <button @click="addCounter">+</button> |
| <button @click="reduceCounter">-</button> |
| </div> |
| </template> |
复制
| |
| |
| |
| import { defineStore } from 'pinia'; |
| |
| export const demoStore = defineStore('demo', { |
| state: () => { |
| return { |
| |
| counter: 0 as Number, |
| mobile:"13111111111" as String |
| } |
| }, |
| getters:{}, |
| actions:{ |
| |
| add(n:number){ |
| this.counter += n; |
| }, |
| reduce(n:number){ |
| this.counter -= n; |
| } |
| } |
| }) |
复制
Getters的使用
| |
| |
| import { defineStore } from 'pinia'; |
| |
| export const demoStore = defineStore('demo', { |
| state: () => { |
| return { |
| |
| counter: 0 as Number, |
| mobile:"13111111111" as String |
| } |
| }, |
| getters:{ |
| mobileHidden:(state):String => { |
| return state.mobile.replace(/(\d{3})\d{4}(\d{4})/g,`$1****$2`); |
| } |
| }, |
| actions:{ |
| |
| add(n:number){ |
| this.counter += n; |
| }, |
| reduce(n:number){ |
| this.counter -= n; |
| } |
| } |
| }) |
复制
| |
| |
| <script lang="ts" setup> |
| import { storeToRefs } from "pinia"; |
| import { demoStore } from '@/store/demo'; |
| |
| const store = demoStore(); |
| |
| const { counter,mobileHidden } = storeToRefs(store); |
| </script> |
| |
| <template> |
| <div class="x-demo-pinia"> |
| pinia测试{{counter}}-{{mobileHidden}} |
| </div> |
| </template> |
| |
| <style lang="less" scoped> |
| |
| </style> |
复制
store互调
在 store 目录下新建 test.ts 作为第二个 store
| |
| |
| import { defineStore } from 'pinia'; |
| |
| export const testStore = defineStore('test', { |
| state: () => { |
| return { |
| list:[1,2,3] as Array<number> |
| } |
| }, |
| getters:{}, |
| actions:{} |
| }) |
复制
| |
| |
| import { defineStore } from 'pinia'; |
| |
| import { testStore } from './test'; |
| import { toRaw } from "vue"; |
| |
| export const demoStore = defineStore('demo', { |
| state: () => { |
| return {} |
| }, |
| getters:{}, |
| actions:{ |
| |
| getList(){ |
| |
| console.log(toRaw(testStore().list)) |
| } |
| } |
| }) |
复制
| <script lang="ts" setup> |
| import { storeToRefs } from "pinia"; |
| import { demoStore } from '@/store/demo'; |
| |
| const store = demoStore(); |
| const { counter,mobileHidden } = storeToRefs(store); |
| |
| const addCounter = () => { |
| store.$patch({ |
| count:store.counter += 2, |
| mobile:"13111111111" |
| }) |
| |
| store.getList(); |
| }; |
| </script> |
| |
| <template> |
| <div class="x-demo-pinia"> |
| pinia测试{{counter}}-{{mobileHidden}} |
| <button @click="addCounter">+</button> |
| </div> |
| </template> |
复制