• Vue第四讲


    计算属性

    computed

    当我们需要对原数据(data)进行一些额外的操纵,希望在原数据的基础之上得到一些新数据,但同时又不希望改变原数据,这种情况下,我们会选择用计算属性

    计算属性的缓存

    首先,先用一段代码演示:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>计算属性</title>
    8. </head>
    9. <body>
    10. <div id="app">
    11. <p>{{cnum}}</p>
    12. <p>{{mnum()}}</p>
    13. <p>{{cnum}}</p>
    14. <p>{{mnum()}}</p>
    15. <p>{{cnum}}</p>
    16. <p>{{mnum()}}</p>
    17. </div>
    18. </body>
    19. <script src="js/vue.min.js"></script>
    20. <script>
    21. new Vue({
    22. el:'#app',
    23. data(){
    24. return {
    25. x:10,
    26. y:20
    27. }
    28. },
    29. // 计算属性
    30. computed: {
    31. cnum(){
    32. console.log('computed');
    33. return this.x+this.y*this.x-this.y;
    34. }
    35. },
    36. methods:{
    37. mnum(){
    38. console.log('methods');
    39. return this.x+this.y*this.x-this.y;
    40. }
    41. }
    42. })
    43. </script>
    44. </html>

    运行结果: 

     从以上结果可以看出,计算属性的结果在第一次计算完成后会缓存下来,在后续的使用,只要原数据没有发生变化,计算属性就不会再重新计算。每一次的使用都是读取缓存中的结果。

    一旦计算属性中依赖的原数据发生变化,计算属性就会重新计算,也就是说,计算属性所对应的方法会重新调用。

    改写Vue案例中的购物车代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>购物车</title>
    8. <style>
    9. table {
    10. border-collapse: collapse;
    11. }
    12. th,
    13. td {
    14. width: 100px;
    15. height: 50px;
    16. text-align: center;
    17. }
    18. </style>
    19. </head>
    20. <body>
    21. <div id="app">
    22. <table>
    23. <thead>
    24. <tr>
    25. <th>商品编号</th>
    26. <th>商品名称</th>
    27. <th>商品单价</th>
    28. <th>商品数量</th>
    29. <th>商品总价</th>
    30. <th>商品操作</th>
    31. </tr>
    32. </thead>
    33. <tbody>
    34. <tr v-if="goodsData.every(item => item.count===0)">
    35. <td>购物车为空</td>
    36. </tr>
    37. <template v-for="(item,index) in filterGoodsData">
    38. <tr :key="item.id">
    39. <td>{{item.id}}</td>
    40. <td>{{item.name}}</td>
    41. <td>{{item.price}}</td>
    42. <td>
    43. <button @click="item.count--">-</button>
    44. <span>{{item.count}}</span>
    45. <button @click="item.count++">+</button>
    46. </td>
    47. <td>{{item.count*item.price}}</td>
    48. <td>
    49. <button @click="goodsData.splice(index,1)">删除</button>
    50. </td>
    51. </tr>
    52. </template>
    53. </tbody>
    54. </table>
    55. <p>合计:{{totalPrice}}</p>
    56. </div>
    57. </body>
    58. <script src="js/vue.min.js"></script>
    59. <script>
    60. new Vue({
    61. el: '#app',
    62. data() {
    63. return {
    64. goodsData: [
    65. { id: 1, name: '苹果', price: 20, count: 1 },
    66. { id: 2, name: '香蕉', price: 10, count: 2 },
    67. { id: 3, name: '橘子', price: 15, count: 4 }
    68. ]
    69. }
    70. },
    71. computed:{
    72. totalPrice(){
    73. return this.goodsData.reduce((sum,item)=>{
    74. return sum+item.price*item.count;
    75. },0)
    76. },
    77. filterGoodsData(){
    78. return this.goodsData.filter(item=>item.count>0);
    79. }
    80. }
    81. })
    82. </script>
    83. </html>

    将数量不为0的商品放到新数组中,原数组不变。

    计算属性的 getter 和 setter

    实例:点击按钮改变值

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>计算属性</title>
    8. </head>
    9. <body>
    10. <div id="app">
    11. <p>{{cnum}}</p>
    12. <button @click="cnum = 'Li Si'">修改</button>
    13. </div>
    14. </body>
    15. <script src="js/vue.min.js"></script>
    16. <script>
    17. new Vue({
    18. el: '#app',
    19. data() {
    20. return {
    21. xing:'Zhang',
    22. ming:'Ling'
    23. }
    24. },
    25. // 计算属性
    26. computed: {
    27. cnum: {
    28. get() {
    29. return this.xing +' '+ this.ming;
    30. },
    31. set(val) {
    32. this.xing = val.split(' ')[0];
    33. this.ming = val.split(' ')[1];
    34. }
    35. }
    36. },
    37. })
    38. </script>
    39. </html>

    点击前:                                                              点击后:

                                                   

     

     

  • 相关阅读:
    蓝牙Mesh系统开发五 ble mesh设备增加与移除
    春秋云境靶场CVE-2022-25578漏洞复现(利用htaccess文件进行任意文件上传)
    Redis-Linux中安装Redis、命令操作Redis
    【工作记录】xpath语法笔记
    丁鹿学堂:前端面试手写系列之promise(一)
    黑马头条(day01)
    国际油气公司数字化转型探析
    小程序中如何使用自定义组件应用及搭建个人中心布局
    Python每日一练(牛客数据分析篇新题库)——第30天:逻辑运算
    大模型全情投入,低代码也越来越清晰
  • 原文地址:https://blog.csdn.net/weixin_52993364/article/details/125504465