怎样解决 [Vue warn]: The computed property "count" is already defined in data. 报错问题?

最近在写vue项目时候发现一个项目中报如下错误

vue.esm.js?5425:578 [Vue warn]: Property or method "counit" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.


found in


---> <App> at src\Vuex.vue
       <Root>

代码如下:

store.js:

import Vue from 'vue'
import Vuex from 'vuex'//记得用cnpm install vuex --save
Vue.use(Vuex)
const state={//状态对象
count:4
}
const mutations={//触发状态
jia(state){
state.count++;
},
jian(state){
state.count--;
}


}
export default new Vuex.Store({



state,
mutations
})

main.js注册:

import Vue from 'vue'
import App from './App'
import router from './router'
import store from'./store'
import vuex from'./Vuex'
Vue.config.productionTip = false

new Vue({

  el: '#app',
   router,
 
  template: '<App/>',
   components: { App },
  store,
  render:xx=>xx(vuex)

})

vuex.vue代码如下:

<template>
<div id="app">
<h1>Hello,World</h1>
<p>{{$store.state.count}}-{{count}} </p>
<button @click="$store.commit('jia')">+</button>
<button @click="$store.commit('jian')">-</button>
</div>
</template>


<script>
export default{
name:'app',


data(){
return {


count:0
}
},
computed:{
count(){
return   this.$store.state.count
}
}
}
</script>


<style>
</style>

我当时搞了半天还是没解决,后来我用有道翻译了此句话The computed property "count" is already defined in data的意思:计算的属性“count”已经在数据中定义。那么照此推理,main.js和store.js没有问题,后来我把计算属性中computed中count改为counit并且把-{{count}}改为-{{counit}}问题解决。这说明计算属性的名字不能与data中属性同名。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值