二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • Shell编程

  • Go编程笔记

  • 前端编程笔记

  • Go学习笔记

  • Vue-21年学习笔记

  • Vue-22年重学笔记

    • 基础知识

      • 初识Vue
      • 模板语法
      • 数据绑定
      • el与data的两种写法
      • MVVM模型
      • 数据代理
      • 事件处理
      • 计算属性
      • 监视属性
      • 绑定样式
      • 条件渲染
      • 列表渲染
        • 列表渲染-基本列表
          • 代码
          • 笔记
        • 列表渲染-key 的原理
          • 代码
          • 笔记
        • 列表过滤
          • 代码
        • 列表排序
          • 代码
        • 更新时的一个问题
          • 代码
        • 监测数据改变的原理_对象
          • 代码
        • 模拟一个数据监测
          • 代码
        • Vue.set 的使用
          • 代码
        • 监测数据改变的原理_数组
          • 代码
        • 总结 Vue 数据监测
          • 代码
      • 收集数据表单
      • 过滤器
      • 内置指令
      • 自定义指令
      • 生命周期
      • 非单文件组件
      • 单文件组件
    • 脚手架工程化

  • 编程世界
  • Vue-22年重学笔记
  • 基础知识
二丫讲梵
2022-08-17
目录

列表渲染

文章发布较早,内容可能过时,阅读注意甄别。

# 列表渲染-基本列表

# 代码

<!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>列表渲染-基本列表</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <!-- 遍历数组 -->
      <h2>测试遍历人员</h2>
      <ul>
        <li v-for="(p,index) in persons" :key="index">{{p.name}}-{{p.age}}</li>
      </ul>
      <!-- 遍历对象 -->
      <h2>汽车信息</h2>
      <ul>
        <li v-for="(value,index) in car" :key="index">{{index}}-{{value}}</li>
      </ul>
      <!-- 遍历字符串 (很少用) -->
      <h2>遍历字符串</h2>
      <ul>
        <li v-for="(value,index) in str" :key="index">{{index}}-{{value}}</li>
      </ul>
      <!-- 指定遍历次数 (很少用) -->
      <h2>指定遍历次数</h2>
      <ul>
        <li v-for="(number,index) in 5" :key="index">{{index}}-{{number}}</li>
      </ul>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示
      new Vue({
        el: "#root",
        data: {
          persons: [
            { id: "001", name: "张三", age: 18 },
            { id: "002", name: "李四", age: 19 },
            { id: "003", name: "王五", age: 20 },
          ],
          car: {
            name: "奥迪A8",
            price: "70w",
            color: "黑色",
          },
          str: "hello",
        },
      });
    </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

# 笔记

v-for 指令:

  1. 用于展示列表数据
  2. 语法:v-for="(item, index) in xxx" : key="yyy"
  3. 可遍历:数组、对象、字符串(用的很少)、指定次数(用的很少)

# 列表渲染-key 的原理

# 代码

<!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>key的原理</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <!-- 遍历数组 -->
      <h2>测试遍历人员</h2>
      <button @click.once="addUser">添加一个老刘</button>
      <ul>
        <li v-for="p in persons" :key="p.id">
          {{p.name}}-{{p.age}}
          <input type="text" />
        </li>
      </ul>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示
      new Vue({
        el: "#root",
        data: {
          persons: [
            { id: "001", name: "张三", age: 18 },
            { id: "002", name: "李四", age: 19 },
            { id: "003", name: "王五", age: 20 },
          ],
        },
        methods: {
          addUser() {
            const p = { id: "004", name: "老刘", age: 40 };
            this.persons.unshift(p); // unshift表示在前边添加
          },
        },
      });
    </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

# 笔记

面试题:react、vue 中的 key 有什么作用?(key 的内部原理)

  1. 虚拟 DOM 中 key 的作用: key 是虚拟 DOM 对象的标识,当数据发生变化时,Vue 会根据【新数据】生成【新的虚拟 DOM】,随后 Vue 进行【新虚拟 DOM】与【旧虚拟 DOM】的差异比较,比较规则如下:
  2. 对比规则:
    1. 旧虚拟 DOM 中找到了与新虚拟 DOM 相同的 key:
    • 若虚拟 DOM 中内容没变, 直接使用之前的真实 DOM!
    • 若虚拟 DOM 中内容变了, 则生成新的真实 DOM,随后替换掉页面中之前的真实 DOM。
    1. 旧虚拟 DOM 中未找到与新虚拟 DOM 相同的 key:
    • 创建新的真实 DOM,随后渲染到到页面。
  3. 用 index 作为 key 可能会引发的问题:
    1. 若对数据进行:逆序添加、逆序删除等破坏顺序操作: 会产生没有必要的真实 DOM 更新 ==> 界面效果没问题, 但效率低。
    2. 如果结构中还包含输入类的 DOM: 会产生错误 DOM 更新 ==> 界面有问题。
  4. 开发中如何选择 key?:
    1. 最好使用每条数据的唯一标识作为 key, 比如 id、手机号、身份证号、学号等唯一值。
    2. 如果不存在对数据的逆序添加、逆序删除等破坏顺序操作,仅用于渲染列表用于展示,使用 index 作为 key 是没有问题的。

# 列表过滤

# 代码

<!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>列表过滤</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <h2>测试遍历人员</h2>
      <input type="text" placeholder="请输入用户名" v-model="keyWord" />
      <ul>
        <li v-for="p in filterPersons" :key="p.id">
          {{p.name}}-{{p.age}}-{{p.sex}}
        </li>
      </ul>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示

      // // 用watch实现
      // new Vue({
      //     el: '#root',
      //     data: {
      //         keyWord:'',
      //         persons:[
      //             {id:'001',name:'马冬梅',age:18,sex:'女'},
      //             {id:'002',name:'周冬雨',age:19,sex:'女'},
      //             {id:'003',name:'周杰伦',age:20,sex:'男'},
      //             {id:'004',name:'温兆伦',age:21,sex:'男'},
      //         ],
      //         filterPersons:[]
      //     },
      //     watch:{
      //         keyWord:{
      //             immediate: true,
      //             handler(val){
      //                 this.filterPersons = this.persons.filter((p)=>{
      //                     return p.name.indexOf(val) !== -1
      //                 })
      //             }
      //         }
      //     }
      // });
      // 用computed实现
      new Vue({
        el: "#root",
        data: {
          keyWord: "",
          persons: [
            { id: "001", name: "马冬梅", age: 18, sex: "女" },
            { id: "002", name: "周冬雨", age: 19, sex: "女" },
            { id: "003", name: "周杰伦", age: 20, sex: "男" },
            { id: "004", name: "温兆伦", age: 21, sex: "男" },
          ],
        },
        computed: {
          filterPersons() {
            return this.persons.filter((p) => {
              return p.name.indexOf(this.keyWord) !== -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
63
64
65
66
67
68
69
70
71

# 列表排序

# 代码

<!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>列表排序</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <h2>测试遍历人员</h2>
      <input type="text" placeholder="请输入用户名" v-model="keyWord" />
      <button @click="sortType = 2">年龄升序</button>
      <button @click="sortType = 1">年龄降序</button>
      <button @click="sortType = 0">原顺序</button>
      <ul>
        <li v-for="p in filterPersons" :key="p.id">
          {{p.name}}-{{p.age}}-{{p.sex}}
        </li>
      </ul>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示

      new Vue({
        el: "#root",
        data: {
          keyWord: "",
          persons: [
            { id: "001", name: "马冬梅", age: 18, sex: "女" },
            { id: "002", name: "周冬雨", age: 29, sex: "女" },
            { id: "003", name: "周杰伦", age: 20, sex: "男" },
            { id: "004", name: "温兆伦", age: 31, sex: "男" },
          ],
          sortType: 0, //0,原顺序,1,年龄降序,2,年龄升序
        },
        computed: {
          filterPersons() {
            const arr = this.persons.filter((p) => {
              return p.name.indexOf(this.keyWord) !== -1;
            });
            // 判断是否需要排序
            if (this.sortType) {
              arr.sort((p1, p2) => {
                return this.sortType === 1 ? p2.age - p1.age : p1.age - p2.age;
              });
            }
            return arr;
          },
        },
      });
    </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

# 更新时的一个问题

# 代码

<!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>更新时的一个问题</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <h2>测试遍历人员</h2>
      <button @click="updateMei">点我更新马冬梅的信息</button>
      <ul>
        <li v-for="p in persons" :key="p.id">{{p.name}}-{{p.age}}-{{p.sex}}</li>
      </ul>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示

      new Vue({
        el: "#root",
        data: {
          persons: [
            { id: "001", name: "马冬梅", age: 18, sex: "女" },
            { id: "002", name: "周冬雨", age: 29, sex: "女" },
            { id: "003", name: "周杰伦", age: 20, sex: "男" },
            { id: "004", name: "温兆伦", age: 31, sex: "男" },
          ],
        },
        methods: {
          updateMei() {
            // this.persons[0].name = '马老师'  // 这种方式奏效
            // this.persons[0].age = 50 // 这种方式奏效
            // this.persons[0].sex = '男' // 这种方式奏效

            // this.persons[0] = {id:'001',name:'马老师',age:50,sex:'男'} // 不奏效

            this.persons.splice(0, 1, {
              id: "001",
              name: "马老师",
              age: 50,
              sex: "男",
            });
          },
        },
      });
    </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

# 监测数据改变的原理_对象

# 代码

<!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>监测数据改变的原理_对象</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <h2>学校名称:{{name}}</h2>
      <h2>学校地址:{{address}}</h2>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示

      const vm = new Vue({
        el: "#root",
        data: {
          name: "尚硅谷",
          address: "北京",
          // 如下信息通过控制台输出 vm 查看
          student: {
            name: "tom",
            age: {
              rAge: 40,
              SAge: 29,
            },
            friends: [{ name: "jerry", age: 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

# 模拟一个数据监测

# 代码

<!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>模拟一个数据监测</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      let data = {
        name: "尚硅谷",
        address: "北京",
      };

      // 创建一个监视的实例对象,用于监视data中属性的变化
      const obs = new Observer(data);
      console.log(obs);

      // 准备一个vm实例对象
      let vm = {};
      vm._data = data = obs;

      function Observer(obj) {
        // 汇总对象中所有的属性形成一个数组
        const keys = Object.keys(obj);
        // 遍历
        keys.forEach((k) => {
          Object.defineProperty(this, k, {
            get() {
              return obj[k];
            },
            set(val) {
              console.log(
                `${k}被改了,我要去解析模板,生成虚拟DOM,开始忙啦。。。`
              );
              obj[k] = val;
            },
          });
        });
      }
    </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

# Vue.set 的使用

# 代码

<!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>Vue.set的使用</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <h1>学校信息</h1>
      <h2>学校名称:{{school.name}}</h2>
      <h2>学校地址:{{school.address}}</h2>
      <h2>学校校长:{{school.leader}}</h2>
      <hr />
      <h1>学生信息</h1>
      <button @click="addSex">添加性别信息</button>
      <h2>姓名:{{student.name}}</h2>
      <h2 v-if="student.sex">性别:{{student.sex}}</h2>
      <h2>年龄:真实{{student.age.rAge}},对外{{student.age.sAge}}</h2>
      <h2>朋友们</h2>
      <ul>
        <li v-for="(f,index) in student.friends" :key="index">
          {{f.name}}-{{f.age}}
        </li>
      </ul>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示

      const vm = new Vue({
        el: "#root",
        data: {
          school: {
            name: "尚硅谷",
            address: "北京",
          },
          // 如下信息通过控制台输出 vm 查看
          student: {
            name: "tom",
            age: {
              rAge: 40,
              sAge: 29,
            },
            friends: [
              { name: "jerry", age: 34 },
              { name: "tony", age: 35 },
            ],
          },
        },
        methods: {
          addSex() {
            // Vue.set(this.student,'sex','男')
            this.$set(this.student, "sex", "男");
          },
        },
      });
    </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

# 监测数据改变的原理_数组

# 代码

<!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>监测数据改变的原理_数组</title>
    <script src="../js/vue.js"></script>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="root">
      <h1>学校信息</h1>
      <h2>学校名称:{{school.name}}</h2>
      <h2>学校地址:{{school.address}}</h2>
      <h2>学校校长:{{school.leader}}</h2>
      <hr />
      <h1>学生信息</h1>
      <button @click="addSex">添加性别信息</button>
      <h2>姓名:{{student.name}}</h2>
      <h2 v-if="student.sex">性别:{{student.sex}}</h2>
      <h2>年龄:真实{{student.age.rAge}},对外{{student.age.sAge}}</h2>
      <h2>爱好</h2>
      <ul>
        <li v-for="(h,index) in student.hobby" :key="index">{{h}}</li>
      </ul>
      <h2>朋友们</h2>
      <ul>
        <li v-for="(f,index) in student.friends" :key="index">
          {{f.name}}-{{f.age}}
        </li>
      </ul>
    </div>

    <script type="text/javascript">
      Vue.config.productionTip = false; // 禁用提示

      const vm = new Vue({
        el: "#root",
        data: {
          school: {
            name: "尚硅谷",
            address: "北京",
          },
          // 如下信息通过控制台输出 vm 查看
          student: {
            name: "tom",
            age: {
              rAge: 40,
              sAge: 29,
            },
            hobby: ["抽烟", "喝酒", "烫头"],
            friends: [
              { name: "jerry", age: 34 },
              { name: "tony", age: 35 },
            ],
          },
        },
        methods: {
          addSex() {
            // Vue.set(this.student,'sex','男')
            this.$set(this.student, "sex", "男");
          },
        },
      });
    </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

# 总结 Vue 数据监测

# 代码

<!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>总结Vue数据监测</title>
    <script src="../js/vue.js"></script>
</head>
<body>

    <!-- 准备好一个容器 -->
    <div id="root">
        <h1>学生信息</h1>
            <button @click="student.age++">年龄+1岁</button> <br/>
             <button @click="addSex">添加性别属性,默认值:男</button> <br/>
             <button @click="student.sex = '未知' ">修改性别</button> <br/>
            <button @click="addFriend">在列表首位添加一个朋友</button> <br/>
            <button @click="updateFirstFriendName">修改第一个朋友的名字为:张三</button> <br/>
            <button @click="addHobby">添加一个爱好</button> <br/>
            <button @click="updateHobby">修改第一个爱好为:开车</button> <br/>
            <button @click="removeSmoke">过滤掉爱好中的抽烟</button> <br/>
            <h3>姓名:{{student.name}}</h3>
            <h3>年龄:{{student.age}}</h3>
            <h3 v-if="student.sex">性别:{{student.sex}}</h3>
            <h3>爱好:</h3>
            <ul>
                <li v-for="(h,index) in student.hobby" :key="index">
                    {{h}}
                </li>
            </ul>
            <h3>朋友们:</h3>
            <ul>
                <li v-for="(f,index) in student.friends" :key="index">
                    {{f.name}}--{{f.age}}
                </li>
            </ul>
        </div>
    </div>

    <script type="text/javascript">
        Vue.config.productionTip = false; // 禁用提示

        const vm = new Vue({
            el: '#root',
            data: {
                // 如下信息通过控制台输出 vm 查看
                student:{
                    name: 'tom',
                    age: 18,
                    hobby:['抽烟','喝酒','烫头'],
                    friends:[
                        {name:'jerry',age:34},
                        {name:'tony',age:35}
                    ]
                }
            },
            methods: {
                addSex(){
                    // Vue.set(this.student,'sex','男')
                    this.$set(this.student,'sex','男')
                },
                addFriend(){
                    this.student.friends.unshift({name:'jack',age:70})
                },
                updateFirstFriendName(){
                    this.student.friends[0].name = '张三'
                    this.student.friends[0].age = 15
                },
                addHobby(){
                    this.student.hobby.push('学习')
                },
                updateHobby(){
                    // this.student.hobby.splice(0,1,'开车')
                    // Vue.set(this.student.hobby,0,'开车')
                    this.$set(this.student.hobby,0,'开车')
                },
                removeSmoke(){
                    this.student.hobby = this.student.hobby.filter((h)=>{
                        return h !== '抽烟'
                    })
                }
            },
        });
    </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
微信 支付宝
上次更新: 2024/06/13, 22:13:45
条件渲染
收集数据表单

← 条件渲染 收集数据表单→

最近更新
01
学习周刊-总第212期-2025年第21周
05-22
02
从赵心童世锦赛夺冠聊聊我的斯诺克情缘
05-16
03
学习周刊-总第211期-2025年第20周
05-15
更多文章>
Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式