Vue-列表渲染

基本列表渲染

v-for指令:

1.用于展示列表数据

2.语法:v-for="(item, index) in xxx",key="yyy"

  • item: 这是当前迭代的对象或数组元素。在每次循环中,item 将代表 对象或数组中的一个元素。
  • index: 这是当前元素在数组或对象中的索引值
  • keykey 是一个特殊的属性,用于帮助 Vue 跟踪每个节点的身份,以便在更新时能够高效地重新渲染。key 的值通常是一个唯一的标识符,通常使用 item 的某个属性(如 item.id)或 index 来确保每个元素都有一个唯一的 key 值。

3.可遍历:数组、对象、字符串(用的很少)、指定次数(用的很少)

基本列表渲染示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript" src="./js/vue.js"></script>
    <div id="app">
        <!-- 遍历数组 -->
         <h3>遍历数组</h3>
        <ul>
            <li  v-for="(item,index) in drugList" :key="index">{{item.name}}-{{item.productionTime}}</li>
        </ul>
        <!-- 遍历对象 -->
         <h3>遍历对象</h3>
        <ul>
            <li v-for="(item,index) in drugInfo">{{index}}-{{item}}</li>
        </ul>
        <!-- 遍历字符串 -->
        <h3>遍历字符串</h3>
        <ul>
            <li v-for="(item,index) in str">{{index}}-{{item}}</li>
        </ul>
        <!-- 遍历指定次数 -->
        <h3>遍历指定次数</h3>
        <ul>
			<li v-for="(number,index) of 5" :key="index">
				{{index}}-{{number}}
			</li>
		</ul>
    </div>

    <script>
        Vue.config.productionTip = false
        Vue.config.devtools = true
        new Vue({
            el:'#app',
            data:{
                drugList:[
                    {id:'01',name:'999感冒灵',productionTime:'20240102'},
                    {id:'02',name:'小柴胡颗粒',productionTime:'20240303'},
                    {id:'03',name:'板蓝根',productionTime:'20240207'},
                    {id:'04',name:'云南白药',productionTime:'20240405'},
                ],
                drugInfo:{
                    name:'云南白药',
                    effictive:'治跌打损伤及创伤创伤'
                },
                str:'abcdefg',
            },
        })
    </script>
</body>

</html>

运行结果:

 key的作用与原理

Vue 中的 key 值主要是供 Vue 内部使用的。它用于帮助 Vue 跟踪每个节点的身份,以便在更新时能够高效地重新渲染组件。

1. 虚拟DOM中key的作用:key是虚拟DOM对象的标识,当数据发生变化时,Vue会根据【新数据】生成【新的虚拟DOM】,随后Vue进行【新虚拟DOM】与【旧虚拟DOM】的差异比较,比较规则如下:

2.对比规则:

  (1).旧虚拟DOM中找到了与新虚拟DOM相同的key:

  • 若虚拟DOM中内容没变, 直接使用之前的真实DOM!
  • 若虚拟DOM中内容变了, 则生成新的真实DOM,随后替换掉页面中之前的真实DOM。

  (2).旧虚拟DOM中未找到与新虚拟DOM相同的key,创建新的真实DOM,随后渲染到到页面。

3.用index作为key可能会引发的问题:

  • 若对数据进行:逆序添加、逆序删除等破坏顺序操作:会产生没有必要的真实DOM更新 ==> 界面效果没问题, 但效率低。
  • 如果结构中还包含输入类的DOM:会产生错误DOM更新 ==> 界面有问题。

4. 开发中如何选择key?:

  • 最好使用每条数据的唯一标识作为key, 比如id、手机号、身份证号、学号等唯一值。
  • 如果不存在对数据的逆序添加、逆序删除等破坏顺序操作,仅用于渲染列表用于展示,使用index作为key是没有问题的。
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript" src="./js/vue.js"></script>
    <div id="app">
        <button @click.once="addDrug">再添加一个药物</button>
        <ul>
            <li  v-for="(item,index) in drugList" :key="index">
                {{item.name}}-{{item.productionTime}}
                <input type="text">
            </li>
        </ul>
    <script>
        Vue.config.productionTip = false
        Vue.config.devtools = true
        new Vue({
            el:'#app',
            data:{
                drugList:[
                    {id:'01',name:'999感冒灵',productionTime:'20240102'},
                    {id:'02',name:'小柴胡颗粒',productionTime:'20240303'},
                    {id:'03',name:'板蓝根',productionTime:'20240207'},
                    {id:'04',name:'云南白药',productionTime:'20240405'},
                ],
            },
            methods: {
                addDrug(){
                    const drugInfo = {
                        id:'05',name:'连花清瘟胶囊',productionTime:'20240607'
                    }
                    this.drugList.unshift(drugInfo)
                }
            },
        })
    </script>
</body>

</html>

这里用index作为key值,点击再添加一个药物后,这里会引发顺序不一致的问题。

 列表过滤

以下是分别使用计算属性和监视属性实现的列表过滤案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript" src="./js/vue.js"></script>
    <div id="app">
        <input type="text" placeholder="请输入药品信息" v-model="keyWord">
        <ul>
            <li v-for="(item,index) in filDrugList" :key="index">
                {{item.name}}-{{item.productionTime}}
            </li>
        </ul>
    </div>
</body>

<script>
    Vue.config.productionTip = false
    Vue.config.devtools = true

    //watch实现列表过滤
    /*  new Vue({
          el:'#app',
          data:{
              keyWord:'',
              drugList:[
                      {id:'01',name:'999感冒灵',productionTime:'20240102'},
                      {id:'02',name:'小柴胡颗粒',productionTime:'20240303'},
                      {id:'03',name:'板蓝根',productionTime:'20240207'},
                      {id:'04',name:'云南白药',productionTime:'20240405'},
                  ],
              filDrugList:[],
          },
          watch:{
               keyWord:{
                  immediate:true,
                  handler(newValue){
                      this.filDrugList = this.drugList.filter((item)=>{
                      return item.name.indexOf(newValue) !== -1
                  })
                  }
               
               }
          }
      })    */
    //computed实现列表过滤
    new Vue({
        el: '#app',
        data: {
            keyWord: '',
            drugList: [
                { id: '01', name: '999感冒灵', productionTime: '20240102' },
                { id: '02', name: '小柴胡颗粒', productionTime: '20240303' },
                { id: '03', name: '板蓝根', productionTime: '20240207' },
                { id: '04', name: '云南白药', productionTime: '20240405' },
            ],
        },
        computed:{
            filDrugList(){
                return this.drugList.filter((item)=>{
                    return item.name.indexOf(this.keyWord) !== -1
                })
            }
        }
    })
</script>

</html>

列表排序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript" src="./js/vue.js"></script>
    <div id="app">
        <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="(item,index) in filDrugList" :key="index">
                {{item.name}}-{{item.productionTime}}
            </li>
        </ul>
    </div>
</body>

<script>
    Vue.config.productionTip = false
    Vue.config.devtools = true
    new Vue({
        el:'#app',
        data:{
            keyWord:'',
            sortType:0,
            drugList:[
                    {id:'01',name:'999感冒灵',productionTime:'20240102'},
                    {id:'02',name:'小柴胡颗粒',productionTime:'20240303'},
                    {id:'03',name:'板蓝根',productionTime:'20240207'},
                    {id:'04',name:'云南白药',productionTime:'20240405'},
                ],
        },
        computed:{
                     filDrugList(){
                        const arr = this.drugList.filter((item)=>{
                            return item.name.indexOf(this.keyWord)!==-1
                        })
                        if(this.sortType){
                            arr.sort((item1,item2)=>{
                                return this.sortType === 1?item2.productionTime-item1.productionTime:item1.productionTime-item2.productionTime
                            })
                        }
                        return arr
                     }
            }
     
    })    
</script>

</html>

列表更新时的一个问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript" src="./js/vue.js"></script>
    <div id="app">
        <button @click="updateDrugInfo">更新云南白药的信息</button>
        <ul>
            <li v-for="(item,index) in drugList" :key="index">
                {{item.name}}-{{item.productionTime}}
            </li>
        </ul>
    </div>
</body>

<script>
    Vue.config.productionTip = false
    Vue.config.devtools = true
    new Vue({
        el:'#app',
        data:{
            keyWord:'',
            sortType:0,
            drugList:[
                    {id:'01',name:'999感冒灵',productionTime:'20240102'},
                    {id:'02',name:'小柴胡颗粒',productionTime:'20240303'},
                    {id:'03',name:'板蓝根',productionTime:'20240207'},
                    {id:'04',name:'云南白药',productionTime:'20240405'},
                ],
        },
        methods: {
				updateDrugInfo(){
					this.drugList[3].name = '连花清瘟胶囊' //奏效
					this.drugList[3].productionTime = '20240608' //奏效
					this.drugList[3] = {id:'04',name:'连花清瘟胶囊',productionTime:'20240608'} //不奏效
					this.drugList.splice(0,1,{id:'04',name:'连花清瘟胶囊',productionTime:'20240608'})
				}
			}
   
     
    })    
</script>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值