当前位置: 首页 > article >正文

vue3安装pinia

vue3安装pinia

在全新的Vue 3项目中安装Pinia并进行基础使用。以下是步骤:

  1. 安装Pinia

在项目根目录下运行以下命令:

npm install pinia
  1. 在main.js中配置Pinia

打开main.js文件,添加以下代码:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const app = createApp(App)
const pinia = createPinia()

app.use(pinia)
app.mount('#app')
  1. 创建一个Store

在src目录下创建一个stores文件夹,然后创建一个新文件,例如counter.js:

import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  actions: {
    increment() {
      this.count++
    }
  },
  getters: {
    doubleCount: (state) => state.count * 2
  }
})
  1. 在组件中使用Store

在任何Vue组件中,您可以这样使用刚创建的store:

<template>
  <div>
    <p>Count: {{ counterStore.count }}</p>
    <p>Double Count: {{ counterStore.doubleCount }}</p>
    <button @click="counterStore.increment">Increment</button>
  </div>
</template>

<script setup>
import { useCounterStore } from '../stores/counter'

const counterStore = useCounterStore()
</script>

http://www.kler.cn/news/334631.html

相关文章:

  • k8s 中微服务之 MetailLB 搭配 ingress-nginx 实现七层负载
  • Oracle创建用户报错-ORA-65096: invalid common user or role name
  • Discord:报错:A fatal Javascript error occured(解决办法)
  • [Python] 《人生重开模拟器》游戏实现
  • 跨境电商中的IP关联及其防范策略
  • 使用Python实现无人机路径规划的灰狼优化算法
  • vue-scrollto实现页面组件锚点定位
  • 使用pytorch进行迁移学习的两个步骤
  • Redis终极入门指南:万字解析帮你从零基础到掌握命令与五大数据结构
  • ARM Assembly 6: Shift 和 Rotate
  • SQL进阶技巧:如何优雅求解指标累计去重问题?
  • SpringBoot在线教育系统:构建与优化
  • react-问卷星项目(6)
  • CMake教程:第一步:一个基本的起点
  • mysql中 and or not的执行顺序
  • 解决Vue应用中遇到路由刷新后出现 404 错误
  • 高等数学 第二讲 数列极限_收敛数列_海涅定理_单调有界准则
  • SkyWalking 高可用
  • Redis SpringBoot项目学习
  • 图文深入理解Oracle Network配置管理(一)