状态管理模式
。文档地址:https://vuex.vuejs.org/zh/installation.html
yarn
yarn add vuex
npm
npm install vuex --save
import store from './store'
new Vue({
store,
render: h => h(App),
}).$mount('#app')
import Vue from 'vue'
import Vuex from 'vuex'
// 挂载Vuex
Vue.use(Vuex)
// 创建VueX对象
const store = new Vuex.Store({
state:{
demo_txt: ''
},
mutations:{
setTxt(state, status){
state.demo_txt = status;
}
}
})
export default store
this.$store.state.demo_txt
this.$store.commit("setTxt", '你好');