计算属性
2019-03-19 本文已影响0人
tmac09
计算属性关键词:computed
methods方法和computed的区别
![](https://img.haomeiwen.com/i16545682/c2dd5f514f3f9651.png)
区别:
可以使用 methods 来替代 computed
computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。
使用 computed 性能会更好,但是如果你不希望缓存,可以使用 methods 属性
练习:购物车价格的计算
![](https://img.haomeiwen.com/i16545682/ae065661c6bbea3a.png)
![](https://img.haomeiwen.com/i16545682/8426f791845a21cb.png)
![](https://img.haomeiwen.com/i16545682/6f2c060396d73370.png)
![](https://img.haomeiwen.com/i16545682/9f07e60de8caa491.png)
练习: 搜索页面
![](https://img.haomeiwen.com/i16545682/fcc23e722b3e4b1e.png)
![](https://img.haomeiwen.com/i16545682/63635aa320da5ca9.png)
![](https://img.haomeiwen.com/i16545682/d188a5dd30ff9b4c.png)
结果:
![](https://img.haomeiwen.com/i16545682/dcb2c51c7630b7b1.png)