vuex

安装依赖 cnpm install --save vuex

在src目录下新建一个store文件夹,文件夹内放置一个index.js文件,引入vue,vuex,使用插件

 

 

 在main.js里引入store,需要全局挂载

 1 import Vue from 'vue'
 2 import App from './App.vue'
 3 
 4 // 引入路由组件
 5 import router from "@/router";
 6 
 7 // 引入仓库
 8 import store from '@/store'
 9 // 三级联动路由组件
10 import TypeNav from '@/components/TypeNav'
11 // 第一个参数:全局组件的名字,第二个参数:哪一个组件
12 Vue.component(TypeNav.name,TypeNav)
13 //全局挂载
14 Vue.prototype.$store=store;
15 import {reqCategory} from '@/api'
16 reqCategory()
17 
18 Vue.config.productionTip = false
19 
20 new Vue({
21 
22   router:router,
23   Store:store,
24   render: h => h(App)
25 }).$mount('#app')

在vuex里保存数据

this.$store.commit('setShopId', e.shopId);

在store里的代码 

const state = {
	shopId: uni.getStorageSync('staff-shop-id') ? uni.getStorageSync('staff-shop-id') : 0,
};
const mutations = {
	
	setShopId(state, param) {
		state.shopId = param
		if (null === param) {
			uni.removeStorageSync('staff-shop-id');
		} else {
			uni.setStorageSync('staff-shop-id', param);
		}
	},

};
export default {
	state,
	mutations
}

  

posted @ 2022-10-25 17:57  小闫的姑娘  阅读(11)  评论(0编辑  收藏  举报