vuex 存值 & 取值 ()

2,765 阅读1分钟

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);

const store = new Vuex.Store({
	state:{
		payPrice:'',
	},
	mutations: {
	  jinqian (state, payload) {
	    state.payPrice = payload
	  }
	}
})

export default store

main.js 引入注册


import store from "./store/index.js"
Vue.prototype.$store = store

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

存值


this.$store.commit('jinqian',this.money);

取值

另一个页面或者组件

created() {
		this.price =this.$store.state.payPrice;
 
},

再此之前 用了 不行的原因:

父组件 与 子组件

globalData:全局变量机制

但是在一些组建中又能拿到 就有点诡异了

// this.price  = getApp().globalData.jt_cost;

在开启子组件的时候父组件已经v-if 关闭掉了 就没法发出事件$emit

 // this.$on('payJiage',(data)=>{
			//  this.price = data
		 // })