uniapp vuex store使用实例

uniapp yekong 1933℃

uniapp 开发时,需要用到全局变量.

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		industryName: '',
		industryId: '',
		userType: 0,
	},
	mutations: {
		industryName(state, industryName) {
			state.industryName = industryName
		},
		industryId(state, industryName) {
			state.industryId = industryId
		},
		userType(state, userType) {
			state.userType = userType
		},
	},
	getters: {
		industryName(state) {
			return state.industryName
		},
		industryId(state) {
			return state.industryId
		},
		userType(state, userType) {
			return state.userType
		},
	},
	actions: {}
})

export default store

main.js

import Vue from 'vue';
import App from './App';
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)

Vue.config.productionTip = false;
import store from './store'
App.mpType = 'app';
const app = new Vue({
	store,
	...App,
});
// 引入请求封装,将app参数传递到配置中
require('./config/request.js')(app)
app.$mount()

使用

	import {
		mapState,
		mapGetters,
		mapMutations
	} from 'vuex';
	computed: mapGetters(['userType'])
	

更新

that.$store.commit('userType', res.data.type);
喜欢 (0)