Vue-02

一、条件渲染

  1. 相关指令
v-if
    <p v-if="tag === 'A'">这里是A</p>
    
v-else-if
    <p v-else-if="tag === 'C'">这里是C</p>
    
v-else
    <p v-else>这里什么也没有</p>

  1. 控制多个元素
<button @click="changels">切换</button>
  1. 复用元素
    Vue为了高效渲染元素,一般会选择复用已存在的元素,而不是从新渲染,如果想每次都重新渲染,那么就给标签一个唯一值的key属性
<template v-if="isUserLogin">
    <label>用户名</label>
    <input type="text" placeholder="请输入用户名" key="userLoginForm">
  </template>
  <template v-else>
    <label>邮箱</label>
    <input type="text" placeholder="请输入邮箱" key="emailLoginForm">
  </template>
  <br>
  <button @click="isUserLogin = !isUserLogin">更换登录方式</button>
  1. v-show
  <p v-show="is">哈哈哈</p>

v-if是真正的条件渲染
v-show只是判断是否将标签隐藏

二、列表渲染

  1. 遍历数组
  <ul>
    <li v-for="(num,index) in numList" :key="index">{{index}}:{{ num }}</li>
  </ul>
  1. 遍历对象
  <ul>
    <li v-for="(item,key,index) in person">{{index}} - {{key}}:{{ item }}</li>
  </ul>
  1. 使用this.$set()或者Vue.set()对数组及对象进行内容的修改
    var app =new Vue ({
      el:'#app',
      data:{
        title:'Vue列表渲染',
        numList:['一','二','三','四','五','六','七','八'],
        person:{name:'李白',age:'45',address:'长安'},
        number:100,
        nameList:[
          {name:'松江',isActive:true},
          {name:'李辉',isActive:false},
          {name:'小明',isActive:true},
          {name:'笑话',isActive:true}
        ]
      },
      methods:{
        changeNumList: function() {
          this.numList.push('十五');管用
          this.numList.reverse();管用
          this.numList.sort();管用

          this.numList='李逵';没用
          this.$set(this.numList,4,'李逵');管用
          Vue.set(this.numList,4,'李逵');管用

          this.numList.length=5;没用
          this.numList.splice(5);管用
        },
        changePerson: function() {
          this,person.age = 100;管用
          delete this.person;没用
          this.person.email = 'libai@qq.com';没用
          Vue.set(this.person,'email','libai@qq.com');管用
          this.person= {name:'杜甫'}管用
        }
      }
    })
  1. 遍历数字
<div>
	<span v-for="n in 10">{{ n }}</span>
</div>
  1. 重复多条内容
<ul>
  <!-- template 不具体实现 -->
  <template v-if="item in items">
    <li>{{item.msg}}</li>
    <li class="divder" role="presentation"></li>
  </template>     
</ul>
  1. v-for与v-if同时使用,v-for优先级高
v-for 与 v-if同时使用,v-for的优先级高
<li v-for="todo in todos" v-if="!todo.isComplete">
  {{todo}}
</li>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值