Vuex的基本使用
1安装vuex的依赖包
npm install vuex --save
2导入vuex的包
如果没有store.js文件可以自己新建。。。
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
3创建store对象
export default new Vuex.Store(
{
state:{
},
mutations:{
},
action:{
}
}
)
4将store对象挂载到vue实例中
new Vue({
el: '#app',
router,
//将创建的共享数据对象,挂载到Vue实例中
//所有的组件,就可以直接从store中获取全局的数据了
store,
components: { App },
template: '<App/>'
})
})
配置好之后全局就可以直接使用了!