美文网首页
Vue总结「一」- 基本使用

Vue总结「一」- 基本使用

作者: loushumei | 来源:发表于2020-10-24 16:19 被阅读0次

1. 插值、指令(模板)

//模板
    1.插值、表达式、
    <p>文本插值:{{message}}</p>
    <p>JS表达式{{flag?'yes':'no'}}(只能是表达式,不能是js语句)</p>

    2.指令、动态属性
    <p :id="myId">动态属性</p>

    3.v-html:会有XSS风险,会覆盖子组件
    <p v-html="myhtml"> 
        <span>有XSS风险</span>
        <span>【注意】使用v-html后,将会覆盖子元素</span>
    </p>

//数据
  data() {
    return {
      message: "hello vue",
      flag:true,
      myId:`id-${Date.now()}`,
      myhtml:"指令-原始html <b>加粗</b> <i>斜体</i>"
    };
  },

2. computed 和 watch

  • computed有缓存,data不变则不会重新计算
<h2>computedDemo</h2>
<p>原值:{{num}}</p>
<p>计算结果:{{double1}}</p>
输入框使用computed双向改变 
<input type="text" v-model="double2">

export default {
  data() {
    return {
      num: 2,
    };
  },
  computed: {
    //普通文本使用computed
    double1() {
      return this.num * 2;
    },
    // input 使用computed,双向改变
    double2:{
      get(){
        return this.num*2
      },
      set(val){
        this.num=val/2
      }
    }
  },
};
  • watch如何深度监听?
  • watch 监听引用类型,拿不到oldVal
<input v-model="name" type="text">
<input v-model="info.city" type="text">
data() {
    return {
        name:'lili',
        info:{
            city:"北京"
        }
    };
},
watch:{
    name(oldVal,val){
        console.log('watch name',oldVal,val)//值类型,可正常拿到
    },
    info:{
        handler(oldVal,val){
            console.log('watch info',oldVal,val)//引用类型 拿不到oldVal,因为引用类型赋值,因为指针相同,指向同一个地址
        },
        deep:true //深度监听
    }
}

3. class和style

  • 使用动态属性
  • 使用驼峰式写法
<p :class="{black:isBlack,yellow:isYellow}">使用class</p>
<p :class="[black,yellow]">使用class</p>
<p :style="styleData">使用style</p>
data() {
    return {
        isBlack:true,
        isYellow:true,

        black:"black",
        yellow:"yellow",

        styleData:{
            fontSize:'40px',//转换为驼峰式
            color:'red',
        }
    };
},

4. 条件渲染

  • v-if v-else 的用法,可以使用变量,也可以使用 === 表达式
<p v-if="type ==='a'">A</p>
<p v-else-if="type ==='b'">B</p>
<p v-else>other</p>

<p v-show="type ==='a'">A by v-show</p>
<p v-show="type ==='b'">B by v-show</p>
  • v-if v-show的区别

v-if 不符合条件的节点不渲染;展示切换不频繁的情况
v-show 不符合条件的节点渲染,但是样式设display:none;不显示在页面上;展示切换频繁的情况

5. 循环(列表)渲染

  • 如何遍历对象? --也可以使用v-for
  • key的重要性。key不能乱写(如random或者index)
  • v-for和v-if不能一起使用
<p>遍历数组</p>
<ul>
    <li v-for="(item,index) in listArr" :key="item.id">
        {{index}} - {{item.id}} -{{item.title}}
    </li>
</ul>
<p>遍历对象</p>
<ul v-if="flag">
    <li v-for="(val,key,index) in listObj" :key="key">
        {{index}} - {{key}} -{{val.title}}
    </li>
</ul>
data() {
    return {
        flag:true,
        listArr:[
            {id:'a',title:"标题1"},
            {id:'b',title:"标题2"},
            {id:'c',title:"标题3"}
        ],
        listObj:{
            a:{title:"标题1"},
            b:{title:"标题2"},
            c:{title:"标题3"},
        }
    };
},

6. 事件

  • event参数,自定义参数

方法没有参数,可以直接获取event对象
方法有参数,event需要手动传入才能获取

<p>{{num}}</p>
<button @click="increment1">+1</button>
<button @click="increment2(2,$event)">+2</button>
methods: {
    increment1(event){
        //当前方法没有参数,可以直接获取event对象
        console.log(event,event.__proto__.constructor) //是原生的 event 对象
        console.log(event.target)
        console.log(event.currentTarget)//事件被注册到当前元素上
        this.num++
        // 1.event是原生的
        // 2.事件被挂载到当前元素
    },
    increment2(val,event){
        //当前方法有参数,event需要手动传入才能获取
        console.log(event.target)
        this.num=this.num+2
    },
},

7. 事件修饰符 & 按键修饰符

.stop
.prevent
.capture
.self
.once
.passive
详情见官方文档

8. 表单

  • v-model

常见表单项 textarea checkbox radio select 详情见官方文档

  • 修饰符 lazy number trim

.lazy 在默认情况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 (除了上述输入法组合文字时)。你可以添加 lazy 修饰符,从而转为在 change 事件之后进行同步:
.trim 自动过滤用户输入的首尾空白字符
.number 自动将用户的输入值转为数值类型

相关文章

网友评论

      本文标题:Vue总结「一」- 基本使用

      本文链接:https://www.haomeiwen.com/subject/pwtwmktx.html