Vue方法vs观察者vs计算的属性

何时使用方法 (When to use methods)

  • To react on some event happening in the DOM

    对DOM中发生的某些事件做出React
  • To call a function when something happens in your component. You can call a methods from computed properties or watchers.

    当组件中发生某些事情时调用函数。 您可以从计算的属性或观察程序中调用方法。

何时使用计算属性 (When to use computed properties)

  • You need to compose new data from existing data sources

    您需要从现有数据源中组合新数据
  • You have a variable you use in your template that’s built from one or more data properties

    您有一个在模板中使用的变量,该变量是根据一个或多个数据属性构建的
  • You want to reduce a complicated, nested property name to a more readable and easy to use one, yet update it when the original property changes

    您希望将复杂的嵌套属性名称简化为更易读和易于使用的名称,但要在原始属性更改时进行更新
  • You need to reference a value from the template. In this case, creating a computed property is the best thing because it’s cached.

    您需要引用模板中的值。 在这种情况下,最好创建一个计算属性,因为它已缓存。
  • You need to listen to changes of more than one data property

    您需要聆听多个数据属性的更改

何时使用观察者 (When to use watchers)

  • You want to listen when a data property changes, and perform some action

    您想在数据属性更改时进行监听,并执行一些操作
  • You want to listen to a prop value change

    您想听听道具价值的变化
  • You only need to listen to one specific property (you can’t watch multiple properties at the same time)

    您只需要听一个特定的属性(您不能同时观看多个属性)
  • You want to watch a data property until it reaches some specific value and then do something

    您要监视一个数据属性,直到达到某个特定值,然后再执行某项操作

翻译自: https://flaviocopes.com/vue-methods-watchers-computed-properties/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值