12_列表渲染

12_列表渲染

v-for

<!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>Document</title>
    <!-- 引入vuejs文件 -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>

  <body>
    <div id="root">
      <h2>人员列表</h2>
      <!-- 遍历数组 -->
      <ul>
        <li v-for="(p,index) in persons" :key="p.id">{{p.name}}-{{p.age}}</li>
      </ul>

      <!-- 遍历对象 -->
      <h2>汽车信息</h2>
      <ul>
        <li v-for="(value,key) of car" :key="key">{{key}}-{{value}}</li>
      </ul>
      <!--遍历字符串  -->
      <h2>遍历字符串</h2>
      <ul>
        <li v-for="(char,index) of str">{{char}}-{{index}}</li>
      </ul>
    </div>
  </body>

  <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: "奥迪",
          price: "70万",
          color: "黑色",
        },
        str: "hello",
      },
    });
  </script>
</html>

: key

开发中如何选择key? :

1.最好使用每条数据的唯一标识作为key,比如id、手机号、身份证号、学号等唯一值。

2.如果不存在对数据的逆序添加、逆序删除等破坏顺序操作,仅用于渲染列表用于展示,使用index作为key是没有问题的。

3.不写: key 默认是index(下标)

列表过滤

<!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>Document</title>
    <!-- 引入vuejs文件 -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>

  <body>
    <div id="root">
      <h2>人员列表</h2>
      <input type="text" placeholder="请输入名字" v-model="keyWord" />
      <ul>
        <li v-for="(p,index) in filePersons" :key="p.id">
          {{p.name}}-{{p.age}}-{{p.sex}}
        </li>
      </ul>
    </div>
  </body>

  <script type="text/javascript">
    Vue.config.productionTip = false;
//用watch实现
//#region
  /*  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: "男" },
        ],
        filePersons: [],
      },
      watch: {
        keyWord: {
          immediate: true, //刚开始就先执行一遍
          handler(val) {
            this.filePersons = this.persons.filter((p) => {
              return p.name.indexOf(val) !== -1;
            });
          },
        },
      },
    });*/
    //#endregion
//用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:{
         filePersons(){
          return  this.filePersons = this.persons.filter((p) => {
              return p.name.indexOf(this.keyWord) !== -1;
            });
         }
     }
    });
  </script>
</html>

列表排序

<!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>Document</title>
    <!-- 引入vuejs文件 -->
    <script type="text/javascript" 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,index) in filePersons" :key="p.id">
          {{p.name}}-{{p.age}}-{{p.sex}}
        </li>
      </ul>
    </div>
  </body>

  <script type="text/javascript">
        Vue.config.productionTip = false;

    //用computed实现
        new Vue({
          el: "#root",
          data: {
            keyWord: '',
            sortType:0,//0原顺序 1降序 2升序
            persons: [
              { id: "001", name: "马冬梅", age: 28, sex: "女" },
              { id: "002", name: "周冬雨", age: 19, sex: "女" },
              { id: "003", name: "周杰伦", age: 10, sex: "男" },
              { id: "004", name: "温兆伦", age: 21, sex: "男" },
            ]
          },
         computed:{
             filePersons(){
              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>
</html>

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>Document</title>
    <!-- 引入vuejs文件 -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>

  <body>
    <div id="root">
      <h2>学生信息</h2>
      <h3>姓名: {{student.name}}</h3>  
      <button @click="addSex">添加一个性别属性,默认是男</button>
      <h3 v-if="student.sex">性别:{{student.sex}}</h3>
    </div>
  </body>

  <script type="text/javascript">
        Vue.config.productionTip = false;

    //用computed实现
        new Vue({
          el: "#root",
          data: {
            student:{
              name:'tom'
            },
          },
          methods:{
              addSex(){
                Vue.set(this.student,'sex','男')
              }
            }
            
        });
  </script>
</html>

小总结

1. vue会监视data中所有层次的数据。

2.如何监测对象中的数据?
通过setter实现监视,且要在new Vue时就传入要监测的数据。	(1).对象中后追加的属性,Vue默认不做响应式处理
	(2).如需给后添加的属性做响应式,请使用如下API:
vue.set(target. propertyName/index, value)或vm.$set(target. propertyName/index, value)

3,如何监测数组中的数据?
通过包裹数组更新元素的方法实现,本质就是做了两件事:
(1).调用原生对应的方法对数组进行更新。
(2).重新解析模板,进而更新页面。

4.在Vue修改数组中的某个元素一定要用如下方法:
	1.使用这些API:plush()、 pop()、 shift()、unshift()、splice()、 sort()、 reverse()
	2.Vue.set()或vm.$set()

特别注意:Vue.set()和 vm.$set()不能给vm或 vm的根数据对象添加属性!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值