学会pinia+创建vue3项目

创建项目

npm init vue@latest
然后写项目名称
进行插件安装 yes/no
npm i 
然后npm run dev  启动非常快

创建store

项目中store 文件夹下counter.js

import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }

  return { count, doubleCount, increment }
})
//相当于定义了一个模块叫counter
//ref相当于state
//computed相当于getters
//function相当于actions
//不存在mutation  区别是actions支持异步

使用

<template>
<h1>{{countStore.count}}</h1>
<button @click="countStore.increment()">+1</button>
</template>
<script setup>
import { useCounterStore } from '../stores/couter'
const countStore = useCounterStore()
</script>

推荐修改state值用actions,不建议直接state.xx = 数据 也可以使用$patch

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值