vuex出错

App.vue
<template>
  <div id="app">
      <p>{{count}}</p>
      <p>
        <button @click="increment">+</button>
        <button @click="decrement">-</button>
      </p>
  </div>
</template>

<script type="text/ecmascript-6">

</script>

<style lang="stylus" rel="stylesheet/stylus">
  #app
    font-family: 'Avenir', Helvetica, Arial, sans-serif
    -webkit-font-smoothing: antialiased
    -moz-osx-font-smoothing: grayscale
    text-align: center
    color: #2c3e50
    font-size : 20px
    line-height 30px
    margin-top: 60px
    background-color : yellow
</style>

main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'

Vue.config.productionTip = false
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment: state => state.count++,
    decrement: state => state.count--
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  template: '<App/>',
  components: { App },
  computed: {
    count () {
      return store.state.count
    }
  },
  methods: {
    increment () {
      store.commit('increment')
    },
    decrement () {
      store.commit('decrement')
    }
  }
})

clipboard.png

阅读 1.9k
1 个回答

简单来说,就是代码块位置不对。。。
参考:Vue-单文件组件
另外可参考一下vue-cli的template~

下面这部分写在main.js中:

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({

state: {
  count: 0
},
mutations: {
  increment: state => state.count++,
  decrement: state => state.count--
}
})

这部分写在App.vue中:

export default{

computed: {
  count () {
    return store.state.count
  }
},
methods: {
  increment () {
    store.commit('increment')
  },
  decrement () {
    store.commit('decrement')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题