请问Vue组件中怎么使用计算属性呀?
发布于 8 年前 作者 dazhihappy 13499 次浏览 来自 问答

请教各位大神,最近再看Vue,写个小demo测试一下,但是不造怎么在Vue组件中使用计算属性、、、

4 回复

computed 属性定义好了之后,正常使用就好了。

<div id="example">
  a={{ a }}, b={{ b }}
</div>

在使用上,a 和 b 没有区别。

var vm = new Vue({
  el: '#example',
  data: {
    a: 1
  },
  computed: {
    // a computed getter
    b: function () {
      // `this` points to the vm instance
      return this.a + 1
    }
  }
})

而 b 是 computed 属性,a 是 普通属性 (data 里定义的)

https://vuejs.org/guide/computed.html

@leapon 嗯,这个我知道。我的意思是在自定义组件里面,怎么使用计算属性 var example-component = Vue.component({ data:function(){ return { a : '' } }, //我的意思是在这儿能使用computed吗?怎么用呢? });

var example-component = Vue.component( { data:function(){ return { a : ‘’ } , computed: { b: function () { return this.a + 1; } } }, });

回到顶部