CNode

请问Vue组件中怎么使用计算属性呀?

问答
Ddazhihappy发布于10 年前最后回复10 年前4 回复14333 浏览0 收藏

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

查看回复

回复 (4)

L
leapon#1·10 年前

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

D
dazhihappy#2·10 年前
引用 leaponcomputed 属性定义好了之后,正常使用就好了。 在使用上,a 和 b 没有区别。 而 b 是 computed 属性,a 是 普通属性 (data 里定义的) https://vuejs...

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

W
wangdashuaihenshuai#3·10 年前

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

参与回复
登录后即可参与回复。登录