vue3-回顾之,状态管理神器 大菠萝- pinia

1.使用 pinia

pnpm i pinia

2.按照工程化目录创建store

import {
    defineStore } from 'pinia';
interface stu {
   
    name: string,
    age: number
}
const  userNumStore=defineStore('numStore',{
   
    //选项api
    actions:{
   
        add(val:number){
   
           this.num=this.num+val
        }

    },
    state() {
   
        return{
   
            num:23,
            stu:{
   
                name:'王祥林',
                age:20

            }

        }
    },

    getters:{
   
        sumNum(state){
   
          return state.num*100+90
        },

        stuInfo():stu{
   
            return this.stu
        }
    }

})

export {
   userNumStore}

3.main.ts中配置大菠萝
import {
    createPinia } from 'pinia'

app.use(createPinia())
4、view层使用
import {
    userNumStore } from "../store/count";

import {
    storeToRefs } from "pinia";

const userNum = userNumStore();

//结构出来的具有响应式
// let { num, stu, stuInfo, sumNum } = storeToRefs(userNum);

let {
    num, stu } = storeToRefs(userNum);



以上是选项式写法

5.下面组合式使用
import {
    ref } from 'vue'

export const userNumStore = defineStore('numStore', () => {
   

    let num = ref(100)

    let stu = ref({
   
        name: '王祥林',
        age: 20

    })
    const add = (val: number) => {
   
        num.value += val + num.value
    }

    return {
    add, num, stu }
})

相关推荐

  1. vue3-回顾状态管理 菠萝- pinia

    2023-12-30 16:42:04       57 阅读
  2. Vue状态管理pinia

    2023-12-30 16:42:04       29 阅读
  3. Vue状态管理Pinia

    2023-12-30 16:42:04       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-30 16:42:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-30 16:42:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-30 16:42:04       87 阅读
  4. Python语言-面向对象

    2023-12-30 16:42:04       96 阅读

热门阅读

  1. CSS学习之-02

    2023-12-30 16:42:04       61 阅读
  2. c++:new和delete的运算符重载

    2023-12-30 16:42:04       57 阅读
  3. k8s报错处理

    2023-12-30 16:42:04       56 阅读
  4. 212. Word Search II

    2023-12-30 16:42:04       56 阅读
  5. 深入浅出理解Web认证:Session、Cookie与Token

    2023-12-30 16:42:04       60 阅读
  6. 二、计算机软件及其使用-文字处理软件 Word 2016

    2023-12-30 16:42:04       65 阅读
  7. Windows自动化之UIautomation(一)

    2023-12-30 16:42:04       51 阅读
  8. 前端网络面试:浏览器输入地址后发生了什么?

    2023-12-30 16:42:04       55 阅读
  9. C语言计算三阶行列式

    2023-12-30 16:42:04       68 阅读
  10. 20世纪40年代是指哪一年

    2023-12-30 16:42:04       235 阅读