Vue3使用Pinia获取全局状态变量

Pinia 是 Vue 3 的状态管理库,用于替代 Vuex。使用 Pinia,你可以轻松地在 Vue 3 应用中管理全局状态。下面是如何使用 Pinia 获取全局状态变量的说明和代码示例:

安装 Pinia

首先,确保你已经安装了 Vue 3 和 Pinia:

npm install vue@next pinia@next

创建 Pinia Store

创建一个 Pinia store 来存储你的全局状态:

javascript// stores/index.js
import { createPinia } from 'pinia';
import app from '../src/app'; // Vue 3 app instance

const pinia = createPinia();
app.use(pinia);

创建 Store

在 stores 目录下创建一个新的 store:

// stores/myGlobalStore.js
import { defineStore } from 'pinia';

export const useMyGlobalStore = defineStore('myGlobalStore', {
state: () => ({
count: 0,
}),
actions: {
increment() {
this.count++;
},
},
});

在组件中使用 Store

现在你可以在任何 Vue 组件中使用这个 store:

// src/components/MyComponent.vue
<template>
<div>
<p>Count: {
   { count }}</p>
<button @click="incrementCount">Increment</button>
</div>
</template>

<script>
import { useMyGlobalStore } from '../stores/myGlobalStore'; // import your store here

export default {
setup() {
const myGlobalStore = useMyGlobalStore(); // use your store here
const incrementCount = () => { myGlobalStore.increment(); }; // use the action in your store here
return { count: myGlobalStore.count, incrementCount }; // expose your state and actions to the template here
},
};
</script>

初始化全局状态

在 src/main.js 或类似的入口文件中,确保你在创建 Vue 实例之前初始化 Pinia store:

import { createApp } from 'vue';
import App from './App.vue'; // your main Vue component here
import { createPinia } from 'pinia'; // import Pinia here if you haven't already done so in your store setup file (step 2)
import { useMyGlobalStore } from './stores/myGlobalStore'; // import your store here if you haven't already done so in your component (step 4) or store setup file (step 3)
// ... other imports ...

相关推荐

  1. Vue3使用Pinia获取全局状态变量

    2024-01-11 08:54:03       54 阅读
  2. Vue3-47-Pinia-修改全局状态变量值的方式

    2024-01-11 08:54:03       59 阅读
  3. Vue3状态管理工具——pinia使用

    2024-01-11 08:54:03       71 阅读

最近更新

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

    2024-01-11 08:54:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 08:54:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 08:54:03       82 阅读
  4. Python语言-面向对象

    2024-01-11 08:54:03       91 阅读

热门阅读

  1. nginx geo模块使用 nginx识别ip归属地做跳转

    2024-01-11 08:54:03       47 阅读
  2. 深度剖析Redis:从基础到高级应用

    2024-01-11 08:54:03       47 阅读
  3. 【技能---Anaconda3常用命令使用入门】

    2024-01-11 08:54:03       48 阅读
  4. windows安装运行Apache James(基于spring的版本)

    2024-01-11 08:54:03       64 阅读
  5. python 安装 cv2报错 conda install PackagesNotFoundError

    2024-01-11 08:54:03       67 阅读
  6. 华纳云:在Conda中环境迁移有哪些步骤

    2024-01-11 08:54:03       52 阅读
  7. conda的安装资源链接

    2024-01-11 08:54:03       61 阅读
  8. achievement_criteria_data

    2024-01-11 08:54:03       57 阅读
  9. Red Hat 8.0 本地源配置方法

    2024-01-11 08:54:03       47 阅读
  10. 迁移学习的最新进展和挑战

    2024-01-11 08:54:03       46 阅读
  11. RTL8762D I2C外设驱动

    2024-01-11 08:54:03       46 阅读
  12. Golang 泛型

    2024-01-11 08:54:03       49 阅读
  13. 03-编码篇-x264编译与介绍

    2024-01-11 08:54:03       54 阅读