Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue 的 computed, watch ,filters 有什么区别 #3

Open
chenhuiYj opened this issue May 26, 2020 · 1 comment
Open

vue 的 computed, watch ,filters 有什么区别 #3

chenhuiYj opened this issue May 26, 2020 · 1 comment

Comments

@chenhuiYj
Copy link
Owner

computed

特性:依赖 Vue 实例,在某一个实例中使用,不接受额外的参数,依赖于 data 里面的值,有缓存机制,可以减少页面调用次数,默认只读,但是可以设置set进行写值。作为实例的一个属性使用。

适用场景:一个属性的值,收到多个属性的值影响的时候。比如购物车的商品总数,总价,全选功能,

watch

特性:依赖 Vue 实例,在某一个实例中使用,回调接收两个参数,一个newVal,一个oldVal,只能监听data里面的值,无缓存机制。

适用场景:一个属性的值的改变会影响多个属性的值的时候。比如根据搜索关键词,给出对应的搜索结果和推荐结果,

filters

特性:可以挂载在Vue实例里面,但不依赖与Vue实例,也可以接受临时变量,可接受参数。无缓存机制。只读不写。只作为一个值的特殊处理方法

适用场景:对一个值进行修饰后显示,比如根据时间戳返回年月日,根据时间戳返回剩余天数,根据返回的数值格式化显示金额

@HSQCoollaughing
Copy link

vue watch也可以监听props

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants