• VUE指令、computed计算属性和watch 侦听器(附带详细案例)



    前言

    为了巩固所学的知识,作者尝试着开始发布一些学习笔记类的博客,方便日后回顾。当然,如果能帮到一些萌新进行新技术的学习那也是极好的。作者菜菜一枚,文章中如果有记录错误,欢迎读者朋友们批评指正。

    一、指令补充

    1. 指令修饰符

    在这里插入图片描述

    • @keyup.enter
    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
    head>
    <body>
      <div id="app">
        <h3>@keyup.enter  →  监听键盘回车事件h3>
        <input @keyup.enter="fn" v-model="username" type="text">
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            username: ''
          },
          methods: {
            fn (e) {
              // if (e.key === 'Enter') {
              //   console.log('键盘回车的时候触发', this.username)
              // }
    
              console.log('键盘回车的时候触发', this.username)
            }
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

    • .trim .number .stop .prevent
    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        .father {
          width: 200px;
          height: 200px;
          background-color: pink;
          margin-top: 20px;
        }
        .son {
          width: 100px;
          height: 100px;
          background-color: skyblue;
        }
      style>
    head>
    <body>
      <div id="app">
        <h3>v-model修饰符 .trim .numberh3>
        姓名:<input v-model.trim="username" type="text"><br>
        年纪:<input v-model.number="age" type="text"><br>
    
        
        <h3>@事件名.stop     →  阻止冒泡h3>
        <div @click="fatherFn" class="father">
          <div @click.stop="sonFn" class="son">儿子div>
        div>
    
        <h3>@事件名.prevent  →  阻止默认行为h3>
        <a @click.prevent href="http://www.baidu.com">阻止默认行为a>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            username: '',
            age: '',
          },
          methods: {
            fatherFn () {
              alert('老父亲被点击了')
            },
            sonFn (e) {
              // e.stopPropagation()
              alert('儿子被点击了')
            }
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    在这里插入图片描述

    2. v-bind对于样式操作的增强 - class

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        .box {
          width: 200px;
          height: 200px;
          border: 3px solid #000;
          font-size: 30px;
          margin-top: 10px;
        }
        .pink {
          background-color: pink;
        }
        .big {
          width: 300px;
          height: 300px;
        }
      style>
    head>
    <body>
    
      <div id="app">
        <div class="box" :class="{ pink: true, big: true }">黑马程序员div>
        <div class="box" :class="['pink', 'big']">黑马程序员div>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
    
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    在这里插入图片描述

    3. 案例 - 京东秒杀 tab 导航高亮

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        * {
          margin: 0;
          padding: 0;
        }
        ul {
          display: flex;
          border-bottom: 2px solid #e01222;
          padding: 0 10px;
        }
        li {
          width: 100px;
          height: 50px;
          line-height: 50px;
          list-style: none;
          text-align: center;
        }
        li a {
          display: block;
          text-decoration: none;
          font-weight: bold;
          color: #333333;
        }
        li a.active {
          background-color: #e01222;
          color: #fff;
        }
    
      style>
    head>
    <body>
    
      <div id="app">
        <ul>
          <li v-for="(item, index) in list" :key="item.id" @click="activeIndex = index">
            <a :class="{ active: index === activeIndex }" href="#">{{ item.name }}a>
          li>
        ul>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            activeIndex: 2, // 记录高亮
            list: [
              { id: 1, name: '京东秒杀' },
              { id: 2, name: '每日特价' },
              { id: 3, name: '品类秒杀' }
            ]
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    在这里插入图片描述

    4. v-bind对于样式操作的增强 - style

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        .progress {
          height: 25px;
          width: 400px;
          border-radius: 15px;
          background-color: #272425;
          border: 3px solid #272425;
          box-sizing: border-box;
          margin-bottom: 30px;
        }
        .inner {
          width: 50%;
          height: 20px;
          border-radius: 10px;
          text-align: right;
          position: relative;
          background-color: #409eff;
          background-size: 20px 20px;
          box-sizing: border-box;
          transition: all 1s;
        }
        .inner span {
          position: absolute;
          right: -20px;
          bottom: -25px;
        }
      style>
    head>
    <body>
      <div id="app">
        
        <div class="progress">
          
          <div class="inner" :style="{ width: percent + '%' }">
            <span>{{ percent }}%span>
          div>
        div>
        <button @click="percent = 25">设置25%button>
        <button @click="percent = 50">设置50%button>
        <button @click="percent = 75">设置75%button>
        <button @click="percent = 100">设置100%button>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            percent: 30
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    在这里插入图片描述

    5. v-model应用于其他表单元素

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        textarea {
          display: block;
          width: 240px;
          height: 100px;
          margin: 10px 0;
        }
      style>
    head>
    <body>
    
      <div id="app">
        <h3>小黑学习网h3>
    
        姓名:
          <input type="text" v-model="username"> 
          <br><br>
    
        是否单身:
          <input type="checkbox" v-model="isSingle"> 
          <br><br>
    
        
        性别: 
          <input v-model="gender" type="radio" name="gender" value="1"><input v-model="gender" type="radio" name="gender" value="2"><br><br>
    
        
        所在城市:
          <select v-model="cityId">
            <option value="101">北京option>
            <option value="102">上海option>
            <option value="103">成都option>
            <option value="104">南京option>
          select>
          <br><br>
    
        自我描述:
          <textarea v-model="desc">textarea> 
    
        <button>立即注册button>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            username: '',
            isSingle: false,
            gender: "2",
            cityId: '102',
            desc: ""
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    在这里插入图片描述

    二、computed计算属性

    1. 基础语法

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        table {
          border: 1px solid #000;
          text-align: center;
          width: 240px;
        }
        th,td {
          border: 1px solid #000;
        }
        h3 {
          position: relative;
        }
      style>
    head>
    <body>
    
      <div id="app">
        <h3>小黑的礼物清单h3>
        <table>
          <tr>
            <th>名字th>
            <th>数量th>
          tr>
          <tr v-for="(item, index) in list" :key="item.id">
            <td>{{ item.name }}td>
            <td>{{ item.num }}个td>
          tr>
        table>
    
        
        <p>礼物总数:{{ totalCount }} 个p>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            // 现有的数据
            list: [
              { id: 1, name: '篮球', num: 1 },
              { id: 2, name: '玩具', num: 2 },
              { id: 3, name: '铅笔', num: 5 },
            ]
          },
          computed: {
            totalCount () {
              // 基于现有的数据,编写求值逻辑
              // 计算属性函数内部,可以直接通过 this 访问到 app 实例
              // console.log(this.list)
    
              // 需求:对 this.list 数组里面的 num 进行求和 → reduce
              let total = this.list.reduce((sum, item) => sum + item.num, 0)
              return total
            }
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    在这里插入图片描述

    2. 计算属性 vS method 方法

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        table {
          border: 1px solid #000;
          text-align: center;
          width: 300px;
        }
        th,td {
          border: 1px solid #000;
        }
        h3 {
          position: relative;
        }
        span {
          position: absolute;
          left: 145px;
          top: -4px;
          width: 16px;
          height: 16px;
          color: white;
          font-size: 12px;
          text-align: center;
          border-radius: 50%;
          background-color: #e63f32;
        }
      style>
    head>
    <body>
    
      <div id="app">
        <h3>小黑的礼物清单🛒<span>{{ totalCountFn() }}span>h3>
        <h3>小黑的礼物清单🛒<span>{{ totalCountFn() }}span>h3>
        <h3>小黑的礼物清单🛒<span>{{ totalCountFn() }}span>h3>
        <h3>小黑的礼物清单🛒<span>{{ totalCountFn() }}span>h3>
        <table>
          <tr>
            <th>名字th>
            <th>数量th>
          tr>
          <tr v-for="(item, index) in list" :key="item.id">
            <td>{{ item.name }}td>
            <td>{{ item.num }}个td>
          tr>
        table>
    
        <p>礼物总数:{{ totalCountFn() }} 个p>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            // 现有的数据
            list: [
              { id: 1, name: '篮球', num: 3 },
              { id: 2, name: '玩具', num: 2 },
              { id: 3, name: '铅笔', num: 5 },
            ]
          },
    
          methods: {
            totalCountFn () {
              console.log('methods方法执行了')
              let total = this.list.reduce((sum, item) => sum + item.num, 0)
              return total
            }
          },
    
          computed: {
            // 计算属性:有缓存的,一旦计算出来结果,就会立刻缓存
            // 下一次读取 → 直接读缓存就行 → 性能特别高
            // totalCount () {
            //   console.log('计算属性执行了')
            //   let total = this.list.reduce((sum, item) => sum + item.num, 0)
            //   return total
            // }
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    在这里插入图片描述

    3. 完整写法

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Documenttitle>
      <style>
        input {
          width: 30px;
        }
      style>
    head>
    <body>
    
      <div id="app">
        姓:<input type="text" v-model="firstName"> +
        名:<input type="text" v-model="lastName"> =
        <span>{{ fullName }}span><br><br>
        
        <button @click="changeName">改名卡button>
      div>
      <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            firstName: '刘',
            lastName: '备',
          },
          methods: {
            changeName () {
              this.fullName = '黄忠'
            }
          },
          computed: {
            // 简写 → 获取,没有配置设置的逻辑
            // fullName () {
            //   return this.firstName + this.lastName
            // }
    
            // 完整写法 → 获取 + 设置
            fullName: {
              // (1) 当fullName计算属性,被获取求值时,执行get(有缓存,优先读缓存)
              //     会将返回值作为,求值的结果
              get () {
                return this.firstName + this.lastName
              },
              // (2) 当fullName计算属性,被修改赋值时,执行set
              //     修改的值,传递给set方法的形参
              set (value) {
                // console.log(value.slice(0, 1))          
                // console.log(value.slice(1))         
                this.firstName = value.slice(0, 1)
                this.lastName = value.slice(1)
              }
            }
          }
        })
      script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    在这里插入图片描述

    4. 成绩案例

    在这里插入图片描述

    • 渲染实现
    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./styles/index.css" />
        <title>Documenttitle>
      head>
      <body>
        <div id="app" class="score-case">
          <div class="table">
            <table>
              <thead>
                <tr>
                  <th>编号th>
                  <th>科目th>
                  <th>成绩th>
                  <th>操作th>
                tr>
              thead>
    
              <tbody v-if="list.length > 0">
                <tr v-for="(item, index) in list" :key="item.id">
                  <td>{{ index + 1 }}td>
                  <td>{{ item.subject }}td>
                  
                  <td :class="{ red: item.score < 60 }">{{ item.score }}td>
                  <td><a href="#">删除a>td>
                tr>
              tbody>
    
              <tbody v-else>
                <tr>
                  <td colspan="5">
                    <span class="none">暂无数据span>
                  td>
                tr>
              tbody>
    
              <tfoot>
                <tr>
                  <td colspan="5">
                    <span>总分:246span>
                    <span style="margin-left: 50px">平均分:79span>
                  td>
                tr>
              tfoot>
            table>
          div>
          <div class="form">
            <div class="form-item">
              <div class="label">科目:div>
              <div class="input">
                <input
                  type="text"
                  placeholder="请输入科目"
                />
              div>
            div>
            <div class="form-item">
              <div class="label">分数:div>
              <div class="input">
                <input
                  type="text"
                  placeholder="请输入分数"
                />
              div>
            div>
            <div class="form-item">
              <div class="label">div>
              <div class="input">
                <button class="submit" >添加button>
              div>
            div>
          div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    
        <script>
          const app = new Vue({
            el: '#app',
            data: {
              list: [
                { id: 1, subject: '语文', score: 62 },
                { id: 7, subject: '数学', score: 39 },
                { id: 12, subject: '英语', score: 70 },
              ],
              subject: '',
              score: ''
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 添加删除
    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./styles/index.css" />
        <title>Documenttitle>
      head>
      <body>
        <div id="app" class="score-case">
          <div class="table">
            <table>
              <thead>
                <tr>
                  <th>编号th>
                  <th>科目th>
                  <th>成绩th>
                  <th>操作th>
                tr>
              thead>
    
              <tbody v-if="list.length > 0">
                <tr v-for="(item, index) in list" :key="item.id">
                  <td>{{ index + 1 }}td>
                  <td>{{ item.subject }}td>
                  
                  <td :class="{ red: item.score < 60 }">{{ item.score }}td>
                  <td><a @click.prevent="del(item.id)" href="http://www.baidu.com">删除a>td>
                tr>
              tbody>
    
              <tbody v-else>
                <tr>
                  <td colspan="5">
                    <span class="none">暂无数据span>
                  td>
                tr>
              tbody>
    
              <tfoot>
                <tr>
                  <td colspan="5">
                    <span>总分:246span>
                    <span style="margin-left: 50px">平均分:79span>
                  td>
                tr>
              tfoot>
            table>
          div>
          <div class="form">
            <div class="form-item">
              <div class="label">科目:div>
              <div class="input">
                <input
                  type="text"
                  placeholder="请输入科目"
                  v-model.trim="subject"
                />
              div>
            div>
            <div class="form-item">
              <div class="label">分数:div>
              <div class="input">
                <input
                  type="text"
                  placeholder="请输入分数"
                  v-model.number="score"
                />
              div>
            div>
            <div class="form-item">
              <div class="label">div>
              <div class="input">
                <button @click="add" class="submit" >添加button>
              div>
            div>
          div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    
        <script>
          const app = new Vue({
            el: '#app',
            data: {
              list: [
                { id: 1, subject: '语文', score: 62 },
                { id: 7, subject: '数学', score: 39 },
                { id: 12, subject: '英语', score: 70 },
              ],
              subject: '',
              score: ''
            },
            methods: {
              del (id) {
                // console.log(id)
                this.list = this.list.filter(item => item.id !== id)
              },
              add () {
                if (!this.subject) {
                  alert('请输入科目')
                  return
                }
                if (typeof this.score !== 'number') {
                  alert('请输入正确的成绩')
                  return
                }
                this.list.unshift({
                  id: +new Date(),
                  subject: this.subject,
                  score: this.score
                })
    
                this.subject = ''
                this.score = ''
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 计算属性
    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./styles/index.css" />
        <title>Documenttitle>
      head>
      <body>
        <div id="app" class="score-case">
          <div class="table">
            <table>
              <thead>
                <tr>
                  <th>编号th>
                  <th>科目th>
                  <th>成绩th>
                  <th>操作th>
                tr>
              thead>
    
              <tbody v-if="list.length > 0">
                <tr v-for="(item, index) in list" :key="item.id">
                  <td>{{ index + 1 }}td>
                  <td>{{ item.subject }}td>
                  
                  <td :class="{ red: item.score < 60 }">{{ item.score }}td>
                  <td><a @click.prevent="del(item.id)" href="http://www.baidu.com">删除a>td>
                tr>
              tbody>
    
              <tbody v-else>
                <tr>
                  <td colspan="5">
                    <span class="none">暂无数据span>
                  td>
                tr>
              tbody>
    
              <tfoot>
                <tr>
                  <td colspan="5">
                    <span>总分:{{ totalScore }}span>
                    <span style="margin-left: 50px">平均分:{{ averageScore }}span>
                  td>
                tr>
              tfoot>
            table>
          div>
          <div class="form">
            <div class="form-item">
              <div class="label">科目:div>
              <div class="input">
                <input
                  type="text"
                  placeholder="请输入科目"
                  v-model.trim="subject"
                />
              div>
            div>
            <div class="form-item">
              <div class="label">分数:div>
              <div class="input">
                <input
                  type="text"
                  placeholder="请输入分数"
                  v-model.number="score"
                />
              div>
            div>
            <div class="form-item">
              <div class="label">div>
              <div class="input">
                <button @click="add" class="submit" >添加button>
              div>
            div>
          div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    
        <script>
          const app = new Vue({
            el: '#app',
            data: {
              list: [
                { id: 1, subject: '语文', score: 62 },
                { id: 7, subject: '数学', score: 89 },
                { id: 12, subject: '英语', score: 70 },
              ],
              subject: '',
              score: ''
            },
            computed: {
              totalScore() {
                return this.list.reduce((sum, item) => sum + item.score, 0)
              },
              averageScore () {
                if (this.list.length === 0) {
                  return 0
                }
                return (this.totalScore / this.list.length).toFixed(2)
              }
            },
            methods: {
              del (id) {
                // console.log(id)
                this.list = this.list.filter(item => item.id !== id)
              },
              add () {
                if (!this.subject) {
                  alert('请输入科目')
                  return
                }
                if (typeof this.score !== 'number') {
                  alert('请输入正确的成绩')
                  return
                }
                this.list.unshift({
                  id: +new Date(),
                  subject: this.subject,
                  score: this.score
                })
    
                this.subject = ''
                this.score = ''
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133

    在这里插入图片描述
    在这里插入图片描述

    三、watch 侦听器

    1. 基础语法

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Documenttitle>
        <style>
          * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-size: 18px;
          }
          #app {
            padding: 10px 20px;
          }
          .query {
            margin: 10px 0;
          }
          .box {
            display: flex;
          }
          textarea {
            width: 300px;
            height: 160px;
            font-size: 18px;
            border: 1px solid #dedede;
            outline: none;
            resize: none;
            padding: 10px;
          }
          textarea:hover {
            border: 1px solid #1589f5;
          }
          .transbox {
            width: 300px;
            height: 160px;
            background-color: #f0f0f0;
            padding: 10px;
            border: none;
          }
          .tip-box {
            width: 300px;
            height: 25px;
            line-height: 25px;
            display: flex;
          }
          .tip-box span {
            flex: 1;
            text-align: center;
          }
          .query span {
            font-size: 18px;
          }
    
          .input-wrap {
            position: relative;
          }
          .input-wrap span {
            position: absolute;
            right: 15px;
            bottom: 15px;
            font-size: 12px;
          }
          .input-wrap i {
            font-size: 20px;
            font-style: normal;
          }
        style>
      head>
      <body>
        <div id="app">
          
          <div class="query">
            <span>翻译成的语言:span>
            <select>
              <option value="italy">意大利option>
              <option value="english">英语option>
              <option value="german">德语option>
            select>
          div>
    
          
          <div class="box">
            <div class="input-wrap">
              <textarea v-model="obj.words">textarea>
              <span><i>⌨️i>文档翻译span>
            div>
            <div class="output-wrap">
              <div class="transbox">meladiv>
            div>
          div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js">script>
        <script>
          // 接口地址:https://applet-base-api-t.itheima.net/api/translate
          // 请求方式:get
          // 请求参数:
          // (1)words:需要被翻译的文本(必传)
          // (2)lang: 需要被翻译成的语言(可选)默认值-意大利
          // -----------------------------------------------
          
          const app = new Vue({
            el: '#app',
            data: {
              // words: ''
              obj: {
                words: ''
              }
            },
            // 具体讲解:(1) watch语法 (2) 具体业务实现
            watch: {
              // 该方法会在数据变化时调用执行
              // newValue新值, oldValue老值(一般不用)
              // words (newValue) {
              //   console.log('变化了', newValue)
              // }
    
              'obj.words' (newValue) {
                console.log('变化了', newValue)
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129

    在这里插入图片描述

    2. 业务实现

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Documenttitle>
        <style>
          * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-size: 18px;
          }
          #app {
            padding: 10px 20px;
          }
          .query {
            margin: 10px 0;
          }
          .box {
            display: flex;
          }
          textarea {
            width: 300px;
            height: 160px;
            font-size: 18px;
            border: 1px solid #dedede;
            outline: none;
            resize: none;
            padding: 10px;
          }
          textarea:hover {
            border: 1px solid #1589f5;
          }
          .transbox {
            width: 300px;
            height: 160px;
            background-color: #f0f0f0;
            padding: 10px;
            border: none;
          }
          .tip-box {
            width: 300px;
            height: 25px;
            line-height: 25px;
            display: flex;
          }
          .tip-box span {
            flex: 1;
            text-align: center;
          }
          .query span {
            font-size: 18px;
          }
    
          .input-wrap {
            position: relative;
          }
          .input-wrap span {
            position: absolute;
            right: 15px;
            bottom: 15px;
            font-size: 12px;
          }
          .input-wrap i {
            font-size: 20px;
            font-style: normal;
          }
        style>
      head>
      <body>
        <div id="app">
          
          <div class="query">
            <span>翻译成的语言:span>
            <select>
              <option value="italy">意大利option>
              <option value="english">英语option>
              <option value="german">德语option>
            select>
          div>
    
          
          <div class="box">
            <div class="input-wrap">
              <textarea v-model="obj.words">textarea>
              <span><i>⌨️i>文档翻译span>
            div>
            <div class="output-wrap">
              <div class="transbox">{{ result }}div>
            div>
          div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js">script>
        <script>
          // 接口地址:https://applet-base-api-t.itheima.net/api/translate
          // 请求方式:get
          // 请求参数:
          // (1)words:需要被翻译的文本(必传)
          // (2)lang: 需要被翻译成的语言(可选)默认值-意大利
          // -----------------------------------------------
          
          const app = new Vue({
            el: '#app',
            data: {
              // words: ''
              obj: {
                words: ''
              },
              result: '', // 翻译结果
              // timer: null // 延时器id
            },
            // 具体讲解:(1) watch语法 (2) 具体业务实现
            watch: {
              // 该方法会在数据变化时调用执行
              // newValue新值, oldValue老值(一般不用)
              // words (newValue) {
              //   console.log('变化了', newValue)
              // }
    
              'obj.words' (newValue) {
                // console.log('变化了', newValue)
                // 防抖: 延迟执行 → 干啥事先等一等,延迟一会,一段时间内没有再次触发,才执行
                clearTimeout(this.timer)
                this.timer = setTimeout(async () => {
                  const res = await axios({
                    url: 'https://applet-base-api-t.itheima.net/api/translate',
                    params: {
                      words: newValue
                    }
                  })
                  this.result = res.data.data
                  console.log(res.data.data)
                }, 300)
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143

    在这里插入图片描述

    3. 完整写法

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Documenttitle>
        <style>
          * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-size: 18px;
          }
          #app {
            padding: 10px 20px;
          }
          .query {
            margin: 10px 0;
          }
          .box {
            display: flex;
          }
          textarea {
            width: 300px;
            height: 160px;
            font-size: 18px;
            border: 1px solid #dedede;
            outline: none;
            resize: none;
            padding: 10px;
          }
          textarea:hover {
            border: 1px solid #1589f5;
          }
          .transbox {
            width: 300px;
            height: 160px;
            background-color: #f0f0f0;
            padding: 10px;
            border: none;
          }
          .tip-box {
            width: 300px;
            height: 25px;
            line-height: 25px;
            display: flex;
          }
          .tip-box span {
            flex: 1;
            text-align: center;
          }
          .query span {
            font-size: 18px;
          }
    
          .input-wrap {
            position: relative;
          }
          .input-wrap span {
            position: absolute;
            right: 15px;
            bottom: 15px;
            font-size: 12px;
          }
          .input-wrap i {
            font-size: 20px;
            font-style: normal;
          }
        style>
      head>
      <body>
        <div id="app">
          
          <div class="query">
            <span>翻译成的语言:span>
            <select v-model="obj.lang">
              <option value="italy">意大利option>
              <option value="english">英语option>
              <option value="german">德语option>
            select>
          div>
    
          
          <div class="box">
            <div class="input-wrap">
              <textarea v-model="obj.words">textarea>
              <span><i>⌨️i>文档翻译span>
            div>
            <div class="output-wrap">
              <div class="transbox">{{ result }}div>
            div>
          div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js">script>
        <script>
          // 需求:输入内容,修改语言,都实时翻译
    
          // 接口地址:https://applet-base-api-t.itheima.net/api/translate
          // 请求方式:get
          // 请求参数:
          // (1)words:需要被翻译的文本(必传)
          // (2)lang: 需要被翻译成的语言(可选)默认值-意大利
          // -----------------------------------------------
       
          const app = new Vue({
            el: '#app',
            data: {
              obj: {
                words: '小黑',
                lang: 'italy'
              },
              result: '', // 翻译结果
            },
            watch: {
              obj: {
                deep: true, // 深度监视
                immediate: true, // 立刻执行,一进入页面handler就立刻执行一次
                handler (newValue) {
                  clearTimeout(this.timer)
                  this.timer = setTimeout(async () => {
                    const res = await axios({
                      url: 'https://applet-base-api-t.itheima.net/api/translate',
                      params: newValue
                    })
                    this.result = res.data.data
                    console.log(res.data.data)
                  }, 300)
                }
              }
    
    
              // 'obj.words' (newValue) {
              //   clearTimeout(this.timer)
              //   this.timer = setTimeout(async () => {
              //     const res = await axios({
              //       url: 'https://applet-base-api-t.itheima.net/api/translate',
              //       params: {
              //         words: newValue
              //       }
              //     })
              //     this.result = res.data.data
              //     console.log(res.data.data)
              //   }, 300)
              // }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152

    在这里插入图片描述
    在这里插入图片描述

    四、综合案例:水果购物车

    在这里插入图片描述

    1. 渲染

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./css/inputnumber.css" />
        <link rel="stylesheet" href="./css/index.css" />
        <title>购物车title>
      head>
      <body>
        <div class="app-container" id="app">
          
          <div class="banner-box"><img src="http://autumnfish.cn/static/fruit.jpg" alt="" />div>
          
          <div class="breadcrumb">
            <span>🏠span>
            /
            <span>购物车span>
          div>
          
          <div class="main" v-if="fruitList.length > 0">
            <div class="table">
              
              <div class="thead">
                <div class="tr">
                  <div class="th">选中div>
                  <div class="th th-pic">图片div>
                  <div class="th">单价div>
                  <div class="th num-th">个数div>
                  <div class="th">小计div>
                  <div class="th">操作div>
                div>
              div>
              
              <div class="tbody">
                <div v-for="(item, index) in fruitList" :key="item.id" class="tr" :class="{ active: item.isChecked }">
                  <div class="td"><input type="checkbox" v-model="item.isChecked" />div>
                  <div class="td"><img :src="item.icon" alt="" />div>
                  <div class="td">{{ item.price }}div>
                  <div class="td">
                    <div class="my-input-number">
                      <button class="decrease"> - button>
                      <span class="my-input__inner">{{ item.num }}span>
                      <button class="increase"> + button>
                    div>
                  div>
                  <div class="td">{{ item.num * item.price }}div>
                  <div class="td"><button>删除button>div>
                div>
              div>
            div>
            
            <div class="bottom">
              
              <label class="check-all">
                <input type="checkbox" />
                全选
              label>
              <div class="right-box">
                
                <span class="price-box">总价  :  ¥ <span class="price">24span>span>
                
                <button class="pay">结算( 6 )button>
              div>
            div>
          div>
          
          <div class="empty" v-else>🛒空空如也div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script>
          const app = new Vue({
            el: '#app',
            data: {
              // 水果列表
              fruitList: [
                {
                  id: 1,
                  icon: 'http://autumnfish.cn/static/火龙果.png',
                  isChecked: true,
                  num: 2,
                  price: 6,
                },
                {
                  id: 2,
                  icon: 'http://autumnfish.cn/static/荔枝.png',
                  isChecked: false,
                  num: 7,
                  price: 20,
                },
                {
                  id: 3,
                  icon: 'http://autumnfish.cn/static/榴莲.png',
                  isChecked: false,
                  num: 3,
                  price: 40,
                },
                {
                  id: 4,
                  icon: 'http://autumnfish.cn/static/鸭梨.png',
                  isChecked: true,
                  num: 10,
                  price: 3,
                },
                {
                  id: 5,
                  icon: 'http://autumnfish.cn/static/樱桃.png',
                  isChecked: false,
                  num: 20,
                  price: 34,
                },
              ],
            },
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119

    2. 删除和修改数量

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./css/inputnumber.css" />
        <link rel="stylesheet" href="./css/index.css" />
        <title>购物车title>
      head>
      <body>
        <div class="app-container" id="app">
          
          <div class="banner-box"><img src="http://autumnfish.cn/static/fruit.jpg" alt="" />div>
          
          <div class="breadcrumb">
            <span>🏠span>
            /
            <span>购物车span>
          div>
          
          <div class="main" v-if="fruitList.length > 0">
            <div class="table">
              
              <div class="thead">
                <div class="tr">
                  <div class="th">选中div>
                  <div class="th th-pic">图片div>
                  <div class="th">单价div>
                  <div class="th num-th">个数div>
                  <div class="th">小计div>
                  <div class="th">操作div>
                div>
              div>
              
              <div class="tbody">
                <div v-for="(item, index) in fruitList" :key="item.id" class="tr" :class="{ active: item.isChecked }">
                  <div class="td"><input type="checkbox" v-model="item.isChecked" />div>
                  <div class="td"><img :src="item.icon" alt="" />div>
                  <div class="td">{{ item.price }}div>
                  <div class="td">
                    <div class="my-input-number">
                      <button :disabled="item.num <= 1" class="decrease" @click="sub(item.id)"> - button>
                      <span class="my-input__inner">{{ item.num }}span>
                      <button class="increase" @click="add(item.id)"> + button>
                    div>
                  div>
                  <div class="td">{{ item.num * item.price }}div>
                  <div class="td"><button @click="del(item.id)">删除button>div>
                div>
              div>
            div>
            
            <div class="bottom">
              
              <label class="check-all">
                <input type="checkbox" />
                全选
              label>
              <div class="right-box">
                
                <span class="price-box">总价  :  ¥ <span class="price">24span>span>
                
                <button class="pay">结算( 6 )button>
              div>
            div>
          div>
          
          <div class="empty" v-else>🛒空空如也div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script>
          const app = new Vue({
            el: '#app',
            data: {
              // 水果列表
              fruitList: [
                {
                  id: 1,
                  icon: 'http://autumnfish.cn/static/火龙果.png',
                  isChecked: true,
                  num: 2,
                  price: 6,
                },
                {
                  id: 2,
                  icon: 'http://autumnfish.cn/static/荔枝.png',
                  isChecked: false,
                  num: 7,
                  price: 20,
                },
                {
                  id: 3,
                  icon: 'http://autumnfish.cn/static/榴莲.png',
                  isChecked: false,
                  num: 3,
                  price: 40,
                },
                {
                  id: 4,
                  icon: 'http://autumnfish.cn/static/鸭梨.png',
                  isChecked: true,
                  num: 10,
                  price: 3,
                },
                {
                  id: 5,
                  icon: 'http://autumnfish.cn/static/樱桃.png',
                  isChecked: false,
                  num: 20,
                  price: 34,
                },
              ],
            },
            methods: {
              del (id) {
                this.fruitList = this.fruitList.filter(item => item.id !== id)
              },
              add (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num++
              },
              sub (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num--
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    3. 全选反选

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./css/inputnumber.css" />
        <link rel="stylesheet" href="./css/index.css" />
        <title>购物车title>
      head>
      <body>
        <div class="app-container" id="app">
          
          <div class="banner-box"><img src="http://autumnfish.cn/static/fruit.jpg" alt="" />div>
          
          <div class="breadcrumb">
            <span>🏠span>
            /
            <span>购物车span>
          div>
          
          <div class="main" v-if="fruitList.length > 0">
            <div class="table">
              
              <div class="thead">
                <div class="tr">
                  <div class="th">选中div>
                  <div class="th th-pic">图片div>
                  <div class="th">单价div>
                  <div class="th num-th">个数div>
                  <div class="th">小计div>
                  <div class="th">操作div>
                div>
              div>
              
              <div class="tbody">
                <div v-for="(item, index) in fruitList" :key="item.id" class="tr" :class="{ active: item.isChecked }">
                  <div class="td"><input type="checkbox" v-model="item.isChecked" />div>
                  <div class="td"><img :src="item.icon" alt="" />div>
                  <div class="td">{{ item.price }}div>
                  <div class="td">
                    <div class="my-input-number">
                      <button :disabled="item.num <= 1" class="decrease" @click="sub(item.id)"> - button>
                      <span class="my-input__inner">{{ item.num }}span>
                      <button class="increase" @click="add(item.id)"> + button>
                    div>
                  div>
                  <div class="td">{{ item.num * item.price }}div>
                  <div class="td"><button @click="del(item.id)">删除button>div>
                div>
              div>
            div>
            
            <div class="bottom">
              
              <label class="check-all">
                <input type="checkbox" v-model="isAll"/>
                全选
              label>
              <div class="right-box">
                
                <span class="price-box">总价  :  ¥ <span class="price">24span>span>
                
                <button class="pay">结算( 6 )button>
              div>
            div>
          div>
          
          <div class="empty" v-else>🛒空空如也div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script>
          const app = new Vue({
            el: '#app',
            data: {
              // 水果列表
              fruitList: [
                {
                  id: 1,
                  icon: 'http://autumnfish.cn/static/火龙果.png',
                  isChecked: true,
                  num: 2,
                  price: 6,
                },
                {
                  id: 2,
                  icon: 'http://autumnfish.cn/static/荔枝.png',
                  isChecked: false,
                  num: 7,
                  price: 20,
                },
                {
                  id: 3,
                  icon: 'http://autumnfish.cn/static/榴莲.png',
                  isChecked: false,
                  num: 3,
                  price: 40,
                },
                {
                  id: 4,
                  icon: 'http://autumnfish.cn/static/鸭梨.png',
                  isChecked: true,
                  num: 10,
                  price: 3,
                },
                {
                  id: 5,
                  icon: 'http://autumnfish.cn/static/樱桃.png',
                  isChecked: false,
                  num: 20,
                  price: 34,
                },
              ],
            },
            computed: {
              // 默认计算属性:只能获取不能设置,要设置需要写完整写法
              // isAll () {
              //   // 必须所有的小选框都选中,全选按钮才选中 → every
              //   return this.fruitList.every(item => item.isChecked)
              // }
              
              // 完整写法 = get + set
              isAll: {
                get () {
                  return this.fruitList.every(item => item.isChecked)
                },
                set (value) {
                  // 基于拿到的布尔值,要让所有的小选框 同步状态
                  this.fruitList.forEach(item => item.isChecked = value)
                }
              }
            },
            methods: {
              del (id) {
                this.fruitList = this.fruitList.filter(item => item.id !== id)
              },
              add (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num++
              },
              sub (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num--
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154

    4. 统计总价

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./css/inputnumber.css" />
        <link rel="stylesheet" href="./css/index.css" />
        <title>购物车title>
      head>
      <body>
        <div class="app-container" id="app">
          
          <div class="banner-box"><img src="http://autumnfish.cn/static/fruit.jpg" alt="" />div>
          
          <div class="breadcrumb">
            <span>🏠span>
            /
            <span>购物车span>
          div>
          
          <div class="main" v-if="fruitList.length > 0">
            <div class="table">
              
              <div class="thead">
                <div class="tr">
                  <div class="th">选中div>
                  <div class="th th-pic">图片div>
                  <div class="th">单价div>
                  <div class="th num-th">个数div>
                  <div class="th">小计div>
                  <div class="th">操作div>
                div>
              div>
              
              <div class="tbody">
                <div v-for="(item, index) in fruitList" :key="item.id" class="tr" :class="{ active: item.isChecked }">
                  <div class="td"><input type="checkbox" v-model="item.isChecked" />div>
                  <div class="td"><img :src="item.icon" alt="" />div>
                  <div class="td">{{ item.price }}div>
                  <div class="td">
                    <div class="my-input-number">
                      <button :disabled="item.num <= 1" class="decrease" @click="sub(item.id)"> - button>
                      <span class="my-input__inner">{{ item.num }}span>
                      <button class="increase" @click="add(item.id)"> + button>
                    div>
                  div>
                  <div class="td">{{ item.num * item.price }}div>
                  <div class="td"><button @click="del(item.id)">删除button>div>
                div>
              div>
            div>
            
            <div class="bottom">
              
              <label class="check-all">
                <input type="checkbox" v-model="isAll"/>
                全选
              label>
              <div class="right-box">
                
                <span class="price-box">总价  :  ¥ <span class="price">{{ totalPrice }}span>span>
                
                <button class="pay">结算( {{ totalCount }} )button>
              div>
            div>
          div>
          
          <div class="empty" v-else>🛒空空如也div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script>
          const app = new Vue({
            el: '#app',
            data: {
              // 水果列表
              fruitList: [
                {
                  id: 1,
                  icon: 'http://autumnfish.cn/static/火龙果.png',
                  isChecked: true,
                  num: 2,
                  price: 6,
                },
                {
                  id: 2,
                  icon: 'http://autumnfish.cn/static/荔枝.png',
                  isChecked: false,
                  num: 7,
                  price: 20,
                },
                {
                  id: 3,
                  icon: 'http://autumnfish.cn/static/榴莲.png',
                  isChecked: false,
                  num: 3,
                  price: 40,
                },
                {
                  id: 4,
                  icon: 'http://autumnfish.cn/static/鸭梨.png',
                  isChecked: true,
                  num: 10,
                  price: 3,
                },
                {
                  id: 5,
                  icon: 'http://autumnfish.cn/static/樱桃.png',
                  isChecked: false,
                  num: 20,
                  price: 34,
                },
              ],
            },
            computed: {
              // 默认计算属性:只能获取不能设置,要设置需要写完整写法
              // isAll () {
              //   // 必须所有的小选框都选中,全选按钮才选中 → every
              //   return this.fruitList.every(item => item.isChecked)
              // }
              
              // 完整写法 = get + set
              isAll: {
                get () {
                  return this.fruitList.every(item => item.isChecked)
                },
                set (value) {
                  // 基于拿到的布尔值,要让所有的小选框 同步状态
                  this.fruitList.forEach(item => item.isChecked = value)
                }
              },
              // 统计选中的总数 reduce
              totalCount () {
                return this.fruitList.reduce((sum, item) => {
                  if (item.isChecked) {
                    // 选中 → 需要累加
                    return sum + item.num
                  } else {
                    // 没选中 → 不需要累加
                    return sum
                  }
                }, 0)
              },
              // 总计选中的总价 num * price
              totalPrice () {
                return this.fruitList.reduce((sum, item) => {
                  if (item.isChecked) {
                    return sum + item.num * item.price
                  } else {
                    return sum
                  }
                }, 0)
              }
    
            },
            methods: {
              del (id) {
                this.fruitList = this.fruitList.filter(item => item.id !== id)
              },
              add (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num++
              },
              sub (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num--
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177

    5. 本地持久化

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="./css/inputnumber.css" />
        <link rel="stylesheet" href="./css/index.css" />
        <title>购物车title>
      head>
      <body>
        <div class="app-container" id="app">
          
          <div class="banner-box"><img src="http://autumnfish.cn/static/fruit.jpg" alt="" />div>
          
          <div class="breadcrumb">
            <span>🏠span>
            /
            <span>购物车span>
          div>
          
          <div class="main" v-if="fruitList.length > 0">
            <div class="table">
              
              <div class="thead">
                <div class="tr">
                  <div class="th">选中div>
                  <div class="th th-pic">图片div>
                  <div class="th">单价div>
                  <div class="th num-th">个数div>
                  <div class="th">小计div>
                  <div class="th">操作div>
                div>
              div>
              
              <div class="tbody">
                <div v-for="(item, index) in fruitList" :key="item.id" class="tr" :class="{ active: item.isChecked }">
                  <div class="td"><input type="checkbox" v-model="item.isChecked" />div>
                  <div class="td"><img :src="item.icon" alt="" />div>
                  <div class="td">{{ item.price }}div>
                  <div class="td">
                    <div class="my-input-number">
                      <button :disabled="item.num <= 1" class="decrease" @click="sub(item.id)"> - button>
                      <span class="my-input__inner">{{ item.num }}span>
                      <button class="increase" @click="add(item.id)"> + button>
                    div>
                  div>
                  <div class="td">{{ item.num * item.price }}div>
                  <div class="td"><button @click="del(item.id)">删除button>div>
                div>
              div>
            div>
            
            <div class="bottom">
              
              <label class="check-all">
                <input type="checkbox" v-model="isAll"/>
                全选
              label>
              <div class="right-box">
                
                <span class="price-box">总价  :  ¥ <span class="price">{{ totalPrice }}span>span>
                
                <button class="pay">结算( {{ totalCount }} )button>
              div>
            div>
          div>
          
          <div class="empty" v-else>🛒空空如也div>
        div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
        <script>
          const defaultArr = [
                {
                  id: 1,
                  icon: 'http://autumnfish.cn/static/火龙果.png',
                  isChecked: true,
                  num: 2,
                  price: 6,
                },
                {
                  id: 2,
                  icon: 'http://autumnfish.cn/static/荔枝.png',
                  isChecked: false,
                  num: 7,
                  price: 20,
                },
                {
                  id: 3,
                  icon: 'http://autumnfish.cn/static/榴莲.png',
                  isChecked: false,
                  num: 3,
                  price: 40,
                },
                {
                  id: 4,
                  icon: 'http://autumnfish.cn/static/鸭梨.png',
                  isChecked: true,
                  num: 10,
                  price: 3,
                },
                {
                  id: 5,
                  icon: 'http://autumnfish.cn/static/樱桃.png',
                  isChecked: false,
                  num: 20,
                  price: 34,
                },
              ]
          const app = new Vue({
            el: '#app',
            data: {
              // 水果列表
              fruitList: JSON.parse(localStorage.getItem('list')) || defaultArr,
            },
            computed: {
              // 默认计算属性:只能获取不能设置,要设置需要写完整写法
              // isAll () {
              //   // 必须所有的小选框都选中,全选按钮才选中 → every
              //   return this.fruitList.every(item => item.isChecked)
              // }
              
              // 完整写法 = get + set
              isAll: {
                get () {
                  return this.fruitList.every(item => item.isChecked)
                },
                set (value) {
                  // 基于拿到的布尔值,要让所有的小选框 同步状态
                  this.fruitList.forEach(item => item.isChecked = value)
                }
              },
              // 统计选中的总数 reduce
              totalCount () {
                return this.fruitList.reduce((sum, item) => {
                  if (item.isChecked) {
                    // 选中 → 需要累加
                    return sum + item.num
                  } else {
                    // 没选中 → 不需要累加
                    return sum
                  }
                }, 0)
              },
              // 总计选中的总价 num * price
              totalPrice () {
                return this.fruitList.reduce((sum, item) => {
                  if (item.isChecked) {
                    return sum + item.num * item.price
                  } else {
                    return sum
                  }
                }, 0)
              }
            },
            methods: {
              del (id) {
                this.fruitList = this.fruitList.filter(item => item.id !== id)
              },
              add (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num++
              },
              sub (id) {
                // 1. 根据 id 找到数组中的对应项 → find
                const fruit = this.fruitList.find(item => item.id === id)
                // 2. 操作 num 数量
                fruit.num--
              }
            },
            watch: {
              fruitList: {
                deep: true,
                handler (newValue) {
                  // 需要将变化后的 newValue 存入本地 (转JSON)
                  localStorage.setItem('list', JSON.stringify(newValue))
                }
              }
            }
          })
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186

    在这里插入图片描述
    在这里插入图片描述

    总结

    欢迎各位留言交流以及批评指正,如果文章对您有帮助或者觉得作者写的还不错可以点一下关注,点赞,收藏支持一下。
    (博客的参考源码可以在我主页的资源里找到,如果在学习的过程中有什么疑问欢迎大家在评论区向我提出)

  • 相关阅读:
    MySQL进阶系列:锁-InnoDB中锁的情况
    力扣21 - 合并两个有序链表【归并排序思维】
    【k8s部署elasticsearch】k8s环境下安装elasticsearch集群和kibana
    ant提供对所有系统属性的访问
    基于Pytorch的动手学习Bert+FC的文本分类模型
    MVC模式
    web前端期末大作业:我的家乡广东(html+css布局)div制作
    ESP8266跟ESP-01S区别
    C++11之lambda表达式理解与使用
    引领未来:AI Native与物联网(IoT)的革命性融合
  • 原文地址:https://blog.csdn.net/HHX_01/article/details/133657685