vue+ts中的pinia使用教程加实际开发方法
- 安装
- 引入
- 使用pinia
- 存储和修改数据
- 【storeToRefs】
- 【getters】使用
- 【$subscribe】
- 开发常用形式
安装
1、安装
复制
引入
2、在main.ts中注册安装pinia
//引入pinia
| import { createPinia } from 'pinia' |
复制
| |
| const app = createApp(App) |
| const pinia = createPinia() |
| |
| app.use(router as any) |
| |
| app.use(pinia) |
| app.mount('#app') |
复制
使用pinia
3、使用pinia
在src文件下创建store文件夹,在文件夹中新建ts文件
存储和修改数据
3.1 存储和修改数据
它有三个概念:state
、getter
、action
,相当于组件中的: data
、 computed
和 methods
。
| |
| *demo.ts |
| */ |
| import {createPinia, defineStore } from "pinia"; |
| |
| export const useDemoStore = defineStore('demo', { |
| persist: true, |
| state: () => ({ |
| demoid: 1 |
| |
| |
| |
| |
| |
| |
| }), |
| |
| actions: { |
| add(value:number) { |
| this.demoid= value |
| }, |
| }, |
| }) |
复制
| |
| |
| |
| <template> |
| <div> |
| {{ demoStore.demoid }} |
| <button @click="add"></button> |
| </div> |
| </template> |
| <script setup lang="ts"> |
| |
| import { useDemoStore } from '@/store/demo' |
| |
| const demoStore = useDemoStore() |
| console.log('11111',demoStore.demoid) |
| |
| function add(){ |
| |
| |
| demoStore.demoid =7 |
| |
| |
| demoStore.$patch({ |
| demoid:7, |
| }) |
| |
| |
| |
| |
| |
| |
| demoStore.add(7) |
| console.log('222',demoStore.demoid) |
| } |
| </script> |
复制
【storeToRefs】
3.2【storeToRefs】
借助storeToRefs
将store
中的数据转为ref
对象,方便在模板中使用。
注意:pinia
提供的storeToRefs
只会将数据做转换,而Vue
的toRefs
会转换store
中数据。
| <template> |
| <div class="count"> |
| <h2>当前求和为:{{demoid}}</h2> |
| </div> |
| </template> |
| |
| <script setup lang="ts" name="Count"> |
| import { useDemoStore} from '@/store/count' |
| |
| import { storeToRefs } from 'pinia' |
| |
| |
| |
| const demoStore =useDemoStore() |
| |
| const {demoid} = storeToRefs(demoStore) |
| </script> |
复制
【getters】使用
3.3【getters】使用
| |
| getters:{ |
| bigSum:(state):number => state.sum *10, |
| upperSchool():string{ |
| return this. school.toUpperCase() |
| } |
| } |
复制
//组件中使用
| <template> |
| <div> |
| {{ demoStore.demoid }} |
| {{ demoStore.bigSum }} |
| <button @click="add"></button> |
| <RouterView></RouterView> |
| </div> |
| </template> |
复制
【$subscribe】
3.4【
s
u
b
s
c
r
i
b
e
】通过
s
t
o
r
e
的
‘
subscribe】 通过 store 的 `
subscribe】通过store的‘subscribe()方法侦听
state` 及其变化
| <script setup lang="ts"> |
| import { useDemoStore } from '@/store/demo' |
| const demoStore = useDemoStore() |
| function add(){ |
| demoStore.$subscribe((mutate,state)=>{ |
| |
| localStorage.setItem('demoid',JSON.stringify(state.demoid)) |
| }) |
| } |
| </script> |
| |
| *demo.ts |
| */ |
| import {createPinia, defineStore } from "pinia"; |
| |
| export const useDemoStore = defineStore('demo', { |
| persist: true, |
| state: () => ({ |
| |
| demoid: JSON.parse(localStorage.getItem('stryyy') as string) || [] |
| }), |
| |
| actions: { |
| add(value:number) { |
| this.demoid= value |
| }, |
| }, |
| }) |
复制
开发常用形式
4、开发常用形式
在src目录下新建文件

加入了pinia持久化存储pinia-plugin-persistedstate 在demo.ts文件中加入persist: true,其余可保持不变
这种方法可以使项目开发更清晰、快捷
| |
| import { createPinia } from 'pinia'; |
| |
| import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' |
| |
| |
| const pinia = createPinia() |
| pinia.use(piniaPluginPersistedstate) |
| |
| |
| export default pinia; |
复制
| |
| import {createPinia, defineStore } from "pinia"; |
| |
| const useDemoStore = defineStore('demo', { |
| persist: true, |
| state: () => ({ |
| demoid: localStorage.getItem('stryyy') as unknown as Number || 0 |
| |
| |
| |
| |
| |
| |
| |
| }), |
| actions: { |
| add(value:number) { |
| this.demoid = value |
| }, |
| }, |
| getters:{ |
| bigSum:(state):number => state.demoid *10, |
| |
| |
| |
| } |
| }) |
| export default useDemoStore; |
复制
将main.ts文件做如下修改
将引入的import { createPinia } from 'pinia’修改为import store from ‘@/store’
app.use(pinia) 修改为app.use(store)
| import './assets/main.css' |
| import { createApp } from 'vue' |
| import App from './App.vue' |
| import router from './router' |
| |
| |
| import store from '@/store' |
| |
| const app = createApp(App) |
| |
| |
| |
| app.use(router as any) |
| |
| |
| app.use(store) |
| app.mount('#app') |
复制