0


浅谈Vue的computed计算属性

文章目录

computed计算属性

1、什么是计算属性

计算属性 本质是方法,只是在使用这些 计算属性 的时候,把他们的名称直接当作 属性 来使用,并不会把 计算属性 当作方法去调用,不需要加小括号 ()调用。

2、为什么要用计算属性

当你需要一个属性是需要经过一些计算的,比如你要一个discounted折扣后的钱属性,现在有price价格,和discount折扣。那么discounted=price*discount。discounted与现有的属性price和discount相关联。

要得出discounted的值,我们可以这样写。

  1. <div>{{price*discount}}</div>

我们不是要 discounted属性吗,这样写貌似不需要添加一个属性,直接用表达计算出折扣后的值就行了。

那么,如果非要得到个discounted呢,我们可能会想到用methods写个方法进行计算

  1. <!--template-->
  2. <div class="price">
  3. 原价:<span v-text="price"></span><br>
  4. 现价: <span v-text="discounted()"></span>
  5. </div>
  6. <!--script-->
  7. data() {
  8. return {
  9. price:100,
  10. discount:0.8
  11. }
  12. },
  13. methods: {
  14. discounted(){
  15. this.price*discount
  16. }
  17. },

再看看vue的comunidad计算属性

  1. <!--template-->
  2. <div class="price">
  3. 原价:<span v-text="price"></span><br>
  4. 现价: <span v-text="discounted"></span>
  5. </div>
  6. <!--script-->
  7. computed: {
  8. discounted(){
  9. return this.price*this.discount
  10. }
  11. },

在这里插入图片描述

我们又会想,用表达式price*discount不就可以得出discounted吗,为什么还要费那么大功夫写什么方法,computed。

那么问题就来了,如果我们的discounted是根据你买的金额,按一下规则来:
原价x折扣0<x<=500.950<x<=1000.85100<x0.8
那么我们该如何实现呢?我们先试着直接用表达式看看。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ieZUyIeJ-1647751350250)(C:\Users\19664\AppData\Roaming\Typora\typora-user-images\image-20220320101855428.png)]

这里报错了,显然不支持多行表达式。如果需要经过一些稍微复杂的计算,我们就必须使用函数了。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g88E56d8-1647751350251)(C:\Users\19664\AppData\Roaming\Typora\typora-user-images\image-20220320102058133.png)]

但是,还是建议即使是简单的表达式,还是建议写成computed或者computed里

因为我们写程序要有关注分离的思想,比如css就写在< style >里,js就写在< script >里,这样更方便我们阅读,也使代码更加规范。


那么,又有问题来了,这里我们的确得出了想要的值,但我们发现用methods不就行了吗,为啥还要computed呢,这两者有什么区别?

1、methods使用时,一般情况需要加括号,而computed则不需要。

2、methods每次调用时会重新执行函数,而computed在其内部变量不变或其返回值不变的情况下多次调用只会执行一次,后续执行时直接从缓存中获取该computed的结果。

至于为什么computed为什么不像methods一样使用小括号调用,是由于computed本身就是一个属性,其本质是computed内部有两个方法(set和get),computed最终的道德的结果是get方法的返回值,而set方法很少使用到,因此简化写法就是上述正常使用computed的格式。

3、compute、methods和watch三者的区别

computedmethodswatch缓存有没有没有异步不行不行行触发模板使用:数据模板使用:方法被监控数据发送变动灵活度最低高最高推荐度最高其次最低(依赖关系容易变得复杂)

4、案例:遍历数组对象的时候进行监视

那么我们一般对数组监视,在遍历的时候对当前数组的对象进行监视,我们该怎么做呢。

computed也是也可以传参的,我们要检测哪个对象,把当前对象传入就可以了,这样检测的数据就是动态的。

我直接用昨天玩css写的例子吧。这里只需要关注价格,其他可以忽略,我也懒得改了,哈哈。

  1. <template>
  2. <div class="container">
  3. <div
  4. class="list"
  5. v-for="item in list"
  6. :key="item"
  7. >
  8. <div class="list-item">
  9. <img
  10. :src="item.url"
  11. alt=""
  12. >
  13. <div class="item-select">
  14. <!-- <div style="width:120px;float:left;">
  15. <button>喜欢</button>
  16. </div>
  17. <div style="width:120px;float:left;">
  18. <button>不喜欢</button>
  19. </div> -->
  20. <svg v-show="!item.like" @click="liked(item)" t="1647706494573" class="icon" viewBox="0 0 1025 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5953" width="200" height="200"><path d="M935.669676 115.583774C871.255802 51.1699 792.995954 24.681952 709.318118 37.925926c-66.219871 10.23398-133.643739 45.751911-196.853616 102.3398C448.652627 83.677837 381.228759 48.159906 315.008888 37.925926 231.331051 24.681952 153.071204 51.1699 88.65733 115.583774 23.641457 180.599647-7.060483 266.08348 1.367501 355.781305c9.029982 89.095826 55.383892 178.191652 134.245738 257.053498 12.039976 12.039976 55.985891 55.985891 100.533804 100.533804l0 0c53.577895 54.179894 108.359788 108.961787 109.563786 110.165785 0.601999 0.601999 1.203998 0.601999 1.805996 1.203998L511.862503 989.084068l48.761905-48.159906 4.815991-4.213992c9.029982 0 265.481481-267.287478 322.069371-324.477366 78.861846-78.861846 125.215755-167.957672 134.245738-257.053498C1031.387489 266.08348 1000.685549 180.599647 935.669676 115.583774zM147.653215 322.67137c-6.019988 18.059965-9.631981 34.915932-10.835979 47.557907-1.805996 13.845973-13.243974 24.079953-27.089947 24.079953l-1.805996 0c-16.855967 0-30.099941-15.651969-27.089947-32.507937 3.611993-21.069959 9.029982-40.333921 15.049971-57.791887 6.019988-16.253968 25.283951-22.875955 40.333921-14.447972 0 0 0.601999 0 0.601999 0C147.051216 296.183422 151.867207 310.029394 147.653215 322.67137zM364.372792 140.867725c0 13.243974-9.029982 24.681952-22.273956 27.089947-79.463845 13.243974-127.623751 48.159906-158.325691 86.687831-8.427984 10.835979-24.079953 13.845973-36.119929 6.621987 0 0-0.601999 0-0.601999 0-13.845973-8.427984-16.855967-27.691946-7.223986-40.93592 60.199882-78.861846 146.285714-103.543798 192.639624-110.767784 16.855967-2.407995 31.303939 10.23398 31.303939 27.089947L363.770793 140.867725z" p-id="5954" fill="#bfbfbf"></path></svg>
  21. <svg v-show="item.like" @click="liked(item)" t="1647706494573" class="icon" viewBox="0 0 1025 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5953" width="200" height="200"><path d="M935.669676 115.583774C871.255802 51.1699 792.995954 24.681952 709.318118 37.925926c-66.219871 10.23398-133.643739 45.751911-196.853616 102.3398C448.652627 83.677837 381.228759 48.159906 315.008888 37.925926 231.331051 24.681952 153.071204 51.1699 88.65733 115.583774 23.641457 180.599647-7.060483 266.08348 1.367501 355.781305c9.029982 89.095826 55.383892 178.191652 134.245738 257.053498 12.039976 12.039976 55.985891 55.985891 100.533804 100.533804l0 0c53.577895 54.179894 108.359788 108.961787 109.563786 110.165785 0.601999 0.601999 1.203998 0.601999 1.805996 1.203998L511.862503 989.084068l48.761905-48.159906 4.815991-4.213992c9.029982 0 265.481481-267.287478 322.069371-324.477366 78.861846-78.861846 125.215755-167.957672 134.245738-257.053498C1031.387489 266.08348 1000.685549 180.599647 935.669676 115.583774zM147.653215 322.67137c-6.019988 18.059965-9.631981 34.915932-10.835979 47.557907-1.805996 13.845973-13.243974 24.079953-27.089947 24.079953l-1.805996 0c-16.855967 0-30.099941-15.651969-27.089947-32.507937 3.611993-21.069959 9.029982-40.333921 15.049971-57.791887 6.019988-16.253968 25.283951-22.875955 40.333921-14.447972 0 0 0.601999 0 0.601999 0C147.051216 296.183422 151.867207 310.029394 147.653215 322.67137zM364.372792 140.867725c0 13.243974-9.029982 24.681952-22.273956 27.089947-79.463845 13.243974-127.623751 48.159906-158.325691 86.687831-8.427984 10.835979-24.079953 13.845973-36.119929 6.621987 0 0-0.601999 0-0.601999 0-13.845973-8.427984-16.855967-27.691946-7.223986-40.93592 60.199882-78.861846 146.285714-103.543798 192.639624-110.767784 16.855967-2.407995 31.303939 10.23398 31.303939 27.089947L363.770793 140.867725z" p-id="5954" fill="#d4237a"></path></svg>
  22. </div>
  23. <div class="price">
  24. 原价:<span v-text="item.price"></span><br>
  25. 现价: <span v-text="discounted(item)"></span><br>
  26. (点亮中间的爱心再减5元!)
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. name: "Child",
  35. data() {
  36. return {
  37. list: [
  38. {
  39. price:88.88,
  40. like: false,
  41. url:
  42. "https://tse4-mm.cn.bing.net/th/id/OIP-C.E5Ce0SanbLrLCq6j5IQXVQHaE7?w=268&h=180&c=7&r=0&o=5&dpr=1.12&pid=1.7",
  43. },
  44. {
  45. price:100,
  46. like: false,
  47. url:
  48. "https://img.zcool.cn/community/0146eb57d154f40000018c1b84142e.jpg@1280w_1l_2o_100sh.jpg",
  49. },
  50. {
  51. price:20.56,
  52. like: false,
  53. url:
  54. "https://img.zcool.cn/community/01e89b5ddfb3c7a80120686b029383.jpg@2o.jpg",
  55. },
  56. {
  57. price:100.50,
  58. like: false,
  59. url:
  60. "https://img.zcool.cn/community/0159bc5767a2600000018c1b76f216.jpg@1280w_1l_2o_100sh.jpg",
  61. },
  62. {
  63. price:666.00,
  64. like: false,
  65. url:
  66. "https://img.zcool.cn/community/0132e85e0abc74a8012165180d2178.jpg@1280w_1l_2o_100sh.jpg",
  67. },
  68. ],
  69. price:100,
  70. discount:0.8
  71. }
  72. },
  73. computed: {
  74. discounted(){
  75. return function(item){
  76. //对价格进行条件计算
  77. let price = item.price
  78. if(0<price&&price<=50){
  79. price = price*0.9
  80. }else if(50<price&&price<=100){
  81. price = price*0.85
  82. }else if(100<price){
  83. price = price*0.8
  84. }
  85. if(item.like){
  86. price -= 5
  87. }
  88. //返回两位小数
  89. return price.toFixed(2)
  90. }
  91. },
  92. },
  93. methods: {
  94. liked(item){
  95. item.like = !item.like
  96. }
  97. },
  98. };
  99. </script>
  100. <style>
  101. body {
  102. background-image: linear-gradient(to top, #c4c5c7 0%, #dcdddf 52%, #ebebeb 100%);
  103. }
  104. .container {
  105. margin: 0 auto;
  106. width: 400px;
  107. /* border: 1px solid black ; */
  108. }
  109. .list-item {
  110. margin-top: 40px;
  111. }
  112. p {
  113. margin: 10px 40%;
  114. }
  115. .list-item img {
  116. width: 100%;
  117. height: 300px;
  118. border-radius: 20px;
  119. box-shadow: 5px 10px 13px 3px rgba(110, 115, 127, 0.5);
  120. opacity: 0.8;
  121. transition: 0.8s;
  122. }
  123. .list-item img:hover {
  124. opacity: 1;
  125. }
  126. .item-select {
  127. width: 100%;
  128. height: 80px;
  129. position: relative;
  130. }
  131. /* .item-select button {
  132. color: white;
  133. font-weight: bold;
  134. font-family: 幼圆;
  135. margin-top: 20px;
  136. width: 100px;
  137. height: 55px;
  138. margin-left: 90px;
  139. background-image: linear-gradient(to top, #a3bded 0%, #6991c7 100%);
  140. padding: 15px;
  141. display: inline;
  142. font-size: 18px;
  143. text-align: center;
  144. border-radius: 10px;
  145. transition: 0.5s;
  146. white-space: nowrap;
  147. border: 1px #dee7ec solid;
  148. opacity: 0.8;
  149. }
  150. button:hover {
  151. background-image: linear-gradient(to right, #ff758c 0%, #ff7eb3 100%);
  152. color: white;
  153. width: 105px;
  154. height: 60px;
  155. } */
  156. .list-item svg{
  157. width: 60px;
  158. height: 60px;
  159. margin: 20px 170px;
  160. position:absolute;
  161. left: 0;
  162. top: 0;
  163. transition: 0.3s;
  164. }
  165. .list-item svg:hover{
  166. width: 65px;
  167. height: 65px;
  168. transition: 0.1s;
  169. }
  170. </style>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X0t5vLjw-1647751350251)(C:\Users\19664\AppData\Roaming\Typora\typora-user-images\image-20220320114231173.png)]

我们来看看是否监视成功

加个点亮爱心再减5元的功能

添加个liked方法,点击了就将当前对象的like取反

在计算属性中再添加一个条件,当点亮了爱心,也就是like=true,就再减5元

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1j36wJXP-1647751350252)(C:\Users\19664\AppData\Roaming\Typora\typora-user-images\image-20220320123219102.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kZyCHmfe-1647751350252)(C:\Users\19664\AppData\Roaming\Typora\typora-user-images\image-20220320123234180.png)]

如果点亮爱心,现价可以再减5元,取消点亮,恢复原来的价格,说明监视成功。

来看看效果吧

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x35C2rqo-1647751350253)(C:\Users\19664\AppData\Roaming\Typora\typora-user-images\image-20220320123559241.png)]

当取消点亮,价格又会恢复。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XFx5RYZl-1647751350253)(C:\Users\19664\AppData\Roaming\Typora\typora-user-images\image-20220320123916499.png)]

标签: vue.js

本文转载自: https://blog.csdn.net/qq_25015861/article/details/123610969
版权归原作者 冰咖啡iii 所有, 如有侵权,请联系我们删除。

“浅谈Vue的computed计算属性”的评论:

还没有评论