uni-app中使用vuex

一、在项目中新建目录 store

在这里插入图片描述

二、在 store 目录下新建 index.js 文件

在这里插入图片描述

三、index.js 文件内容如下

// 导入vue模块
import Vue from 'vue'
// 导入vuex模块
import Vuex from 'vuex'
// 导入外部vuex核心属性
import state from './state.js'
import getters from './getters.js'
import actions from './actions.js'
import mutations from './mutations.js'

// 使用 Vue.use() 方法安装插件
Vue.use(Vuex)

// 创建 Vuex.Store() 实例对象
const store = new Vuex.Store({
    
    
	state,
	getters,
	actions,
	mutations
})

// 导出,以便外部文件使用
export default store

四、在 main.js 文件中导入并注册到vue实例中

import Vue from 'vue'
import App from './App'
import store from './store'

Vue.prototype.$store = store

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
    
    
	store,
	...App
})
app.$mount()

猜你喜欢

转载自blog.csdn.net/qq_40117020/article/details/109076468