uni中使用store是和vue是一样的道理,知道vue中store的使用 uni里便也是相同的方法

一、在根目录创建store.js

//引用Vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

//实例store对象
const store = new Vuex.Store({
	state: {
		proid: 123,
	},
    //mutations是修改store数据入口
	mutations: {
		proid(state, msg) {
			state.proid = msg;
		},
	}
})

//导出store对象
export default store

二、在main.js中全局引入store

这样就可以使用store中存的数据了

另外如果要对store中的数据进行修改如下:

//chackbtn是需要发生修改时绑定的事件
//通过commit触发store中muations里定义的方法 实现store里数据的修改
checkbtn(index,id,name){ 
		this.$store.commit("proid",id)
    },

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐