import Vue from 'vue'import App from './App.vue'
Vue.config.productionTip =falseimport Vuex from 'vuex'
Vue.use(Vuex);
// 创建store对象
const store = new Vuex.Store({
// 放数据的
state: {
count: 0
},
// 放方法
mutations: {
// 方法是用来修改state里的数据的
increment(state){
state.count++
},
// 你给我一个数字我就加多少
// 参数1必须是state
// 把你的参数写到参数2
// vuex最多只能传一个参数,如果非要传多个,就包成对象或者数组
addNum(state,num){
window.console.log(num,num2);
state.count += num;},
addYidui(state,nums){
for(var i = 0; i < nums.length; i++){
state.count += nums[i];}}}})
new Vue({
store,
render: function(h){return h(App)}}).$mount('#app')
app.vue
<template><div><h1>我是app.vue</h1><p>store里的count是:{{$store.state.count }}</p><button @click="$store.commit('increment')">修改count的值</button><son></son></div></template><script>import son from './son.vue'export default {
components:{
son
}}</script><style></style>