1.vuex是干什么的
vuex是一种状态管理模式,简单来说,就是把多个组件需要共享的变量全部存储在一个store对象中,然后,把这个store对象挂载到顶层vue实例中,让所有组件都可以共享这个对象中的所有变量属性
2.vuex的初使用
2.1安装
注意Vuex是个插件,你要想用,用脚手架初始化项目之后,得install
npm install vuex –save
2.2初使用
在入口文件挂载store之后,vue默认做了一步:
vue.prototype.$store = store
所有组件都可以通过this.$store.state来使用啦
import Vue from 'vue'
import App from './App'
import store from './store/index' //在入口文件引入store
Vue.config.productionTip = false
new Vue({
el: '#app',
store, //然后在这里挂载,
render: h => h(App)
})
在src下创建名为store的文件夹,定义store对象来存放我们的状态