pinia状态管理使用
1。安装
npm install pinia
创建文件夹和文件
代码
// stores/counter.js
import { defineStore } from 'pinia'export const useCounterStore = defineStore('counter', {state: () => {return { count: 0,data:{}}},// 也可以这样定义// state: () => ({ count: 0 })actions: {increment() {this.count++},},
})
3、使用
main.js文件引入
// 状态管理
import { createPinia } from 'pinia'
const pinia = createPinia()app.use(pinia)
使用,下面赋值,页面就可以这样用拉
{{ counter.data.name}}
import { useCounterStore } from '../../../stores/counter'// 获取 store 实例const counter = useCounterStore()counter.data.name = "666666"