【vue3中状态管理工具pinia的使用】pinia状态三(actions)

csdn关于pinia学习文章
index.js

有三个状态:state、getters 和 actions 并且可以安全地假设这些概念等同于组件中的“数据”、“计算”和“方法”。
import {defineStore} from 'pinia';
 
export const  useStore = defineStore('main',{
    // 定义state
    state:( )=>{
        return {
           goodsList : [
            { id: 1, title: "手机", price: 100, num: 1, checked: false },
            { id: 2, title: "平板", price: 500, num: 1, checked: false },
            { id: 3, title: "耳机", price: 200, num: 1, checked: false },
          ],
          age:18,
        }
    },
    actions:{   //Actions 相当于组件中的 methods。
        addAge(state){
            this.age++;
            console.log(this.goodsList);
        }
    }
})

运用

<a-button @click="store.addAge">修改age</a-button>

import { useStore } from "../store/index";
const store = useStore()

效果
在这里插入图片描述

你可能感兴趣的:(pinia,actions)