在 vuepress 中使用 vuex

3/21/2021 vuepressvue

在 vue 注册并使用 vuex

参考文章

Personally, is it possible to install Vuex in a project with Vuepress, how and where do I do it? · Issue #1514 · vuejs/vuepress (opens new window)

# 注册

enhanceApp.js

import Vuex from 'vuex';
import store from './store/index.js';

export default ({ Vue }) => {
  Vue.use(Vuex);
  Vue.mixin({ store: store });
};

其中 vuex 数据定义在 ./store/index.js 中。

# 定义

./store/index.js 文件中的定义就是普通的 vuex 数据定义了,demo:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const projectConst = require("../project-const.js")

export default new Vuex.Store({
  state:{
      basePath: projectConst.base
  }
})

# 使用

在所有组件中,都可以使用 this.$store.xxx 使用 vuex

更新时间: Sunday, March 21, 2021 20:54