npm config set registry https://[registry.npm.taobao.org](http://registry.npm.taobao.org/)
npm config get registry//配置后可通过下面方式来验证是否成功
npm install -g cnpm --registry=[https://registry](https://registry/).npm.taobao.org
//cnpm安装脚手架
cnpm install -g vue-cli
vue init webpack my-vue
cd my-vue
cnpm install
cnpm run dev
脚手架安装好后,再安装vuex
cnpm install vuex --save
五、如何使用Vuex
1.如何通过Vuex来实现如下效果?
①创建一个store.js文件
import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
const store = new Vuex.Store({
state: { //这里的state必须是JSON,是一个对象
count: 1 //这是初始值
},
mutations: {//突变,罗列所有可能改变state的方法
add(state) {
state.count++; //直接改变了state中的值,而并不是返回了一个新的state
},
reduce(state){
state.count--;
}
}
});
export default store;//用export default 封装代码,让外部可以引用
②在main.js文件中引入store.js文件
import store from "./vuex/store"
new Vue({
router,
store,
el: '#app',
render: h => h(App)
})
③新建一个模板count.vue
{{msg}}
{{$store.state.count}}-{{count}}
//这两种写法都可以
由于 store 中的状态是响应式的,当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。 在组件中调用 store 中的状态简单到仅需要在计算属性中返回即可。改变store 中的状态的唯一途径就是显式地提交 (commit) mutations。