概述:vuex就是spa中的全局变量,数据更新,视图全部自动更新
1,安装vuex
cnpm install vuex --save
2,在main.js下导入并使用vuex
import Vuex from ‘vuex’
Vue.use(Vuex);
3,创建vuex变量
var store=new Vuex.Store({
state:{
age:25,
}
})
4,引入vuex变量
new Vue({
store:store,
render: h => h(App)
}).$mount('#app')
5,在需要的组件中,使用
.
.
<p>全局变量:{{age}}</p>
.
.
export default {
components: {
FooterCom
},
computed:{
age:function(){
return this.$store.state.age
}
}
}
读数据:
computed:{
age:function(){
return this.$store.state.age;
}
}
写数据
this.$store.state.age=90;