何时使用方法 (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/