Vue-基础语法(一)

2019-06-17  本文已影响0人  zhouhao_180

一、了解组件、库、框架的区别?

前端框架、组件与库的区别(转) :https://blog.csdn.net/a519781181/article/details/78975689

二、Vue 是什么?

三、Vue基本使用

1、使用Vue将helloworld 渲染到页面上

<div id="app">
    <div>{{msg}}</div>
</div>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            msg: 'Hello World'
        }
    })
</script>
// el: 标签选择器(css选择器)
// data: 模型数据

四、Vue模板语法

1、前端渲染三种方式

2、vue模板语法组成

3、插值表达式

 <div> {{ "Hello World" + 123 + "你好" }} 我是字符串 </div>

4、指令

4.1、v-cloak(解决插值表达式闪烁问题)

<style type="text/css">
  1、通过属性选择器选择到带有属性 v-cloak 的标签,让他隐藏
  [v-cloak] {
      display: none;
  }
</style>
<div id="app">
    2、 让带有插值语法的标签添加 v-cloak 属性,在数据渲染完之后,v-cloak 属性会被自动去除, 
     v-cloak一旦移除也就是没有这个属性了,属性选择器就选择不到该标签,
     也就是对应的标签会变为可见
<div v-cloak>{{msg}}</div>
</div>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript">
    var vm = new Vue({
        //  el   指定元素 id 是 app 的元素
        el: '#app',
        //  data  里面存储的是数据
        data: {
            msg: 'Hello Vue'
        }
    });
</script>

4.2、v-text (没有闪烁问题)

<div id="app">
    注意:在指令中不要写插值语法,直接写对应的变量名称,在 v-text 中,赋值的时候不要在写 插值语法 
    一般属性中不加 {{}}  直接写 对应 的数据名 
    <p v-text="msg"></p>
    <p>
        <!-- Vue 中只有在标签的 内容中 才用插值语法 -->
        {{msg}}
    </p>
</div>
​
<script>
    new Vue({
        el: '#app',
        data: {
            msg: 'Hello Vue.js'
        }
    });​
</script>

4.3、v-html

<div id="app">
    <p v-html="html"></p>
    输出:html标签在渲染的时候被解析

    <p>{{message}}</p>
    输出:<span>通过双括号绑定</span>

    <p v-text="text"></p>
    输出:<span>html标签在渲染的时候被源码输出</span>
</div>
<script>
    let app = new Vue({  
        el: "#app",
          data: {    
            message: "<span>通过双括号绑定</span>",
            html: "<span>html标签在渲染的时候被解析</span>",
            text: "<span>html标签在渲染的时候被源码输出</span>", 
        } 
    });
</script>

4.4、v-pre

<span v-pre>{{ this will not be compiled }}</span>
显示的是{{ this will not be compiled }} 

<span v-pre>{{msg}}</span>
即使data里面定义了msg这里仍然显示 {{msg}}

<script>
    new Vue({
        el: '#app',
        data: {
            msg: 'Hello Vue.js'
        }
    });​
</script>

总结:插值表达式、v-text、v-pre

插值表达式 v-text v-html v-pre
填充纯文本,支持简单计算 填充纯文本,语法简洁 填充HTML片段,存在安全问题,本网站内部数据可以用,第三方数据不可用 填充原始信息 显示原始信息 跳过vue编译成原生代码的过程
只能写在标签中间,不能给属性绑定值 会覆盖标签中原内容 会覆盖原内容

4.5、数据响应式

数据驱动内容变化,不需要显示地操作DOM

4.6、v-once

即使data里面定义了msg 后期我们修改了 仍然显示的是第一次data里面存储的数据即 Hello Vue.js 

<span v-once>{{ msg }}</span>
<script>
 new Vue({
    el: '#app',
     data: {
         msg: 'Hello Vue.js'
     }
 });
</script>

4.7、双向数据绑定

4.8、v-model

<div id="app">
    <div>{{msg}}</div>
    <div>
        当输入框中内容改变的时候,数据模型中的mgs值会自动改变,
        而mgs值改变后,页面标签上的 msg 会自动更新
        <input type="text" v-model='msg'>
    </div>
</div>
<script>
    let app = new Vue({  
        el: "#app",
          data: {    
            msg: "Hello World"
        } 
    });
</script>

4.9、MVC和MVVM

4.10、v-on

v-on事件函数写法

<div id="app">
    <div>{{num}}</div>
    <div>
        如果事件直接绑定函数名称,那么默认会传递事件对象作为事件函数的第一个参数
        <button @click='handle1'>点击1</button>

        如果事件绑定函数调用,那么事件对象必须作为最后一个参数显示传递,并且事件对象的名称必须是$event
        <button @click='handle2(123, 456, $event)'>点击2</button>
    </div>
</div>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript">
    var vm = new Vue({
        el: '#app',
        data: {
            num: 0
        },
        methods: { 
            handle1: function(event) { // 默认传递event
                console.log(event.target.innerHTML)
            },
            handle2: function(p, p1, event) {
                console.log(p, p1)
                console.log(event.target.innerHTML)
                this.num++;
            }
        }
    });
</script>

注意:

  • vue中的event对象和原生的是相同的
  • methods 是一个对象
  • 在methods中使用data中的数据需要加this.访问,this指向实例对象vm
  • 箭头函数会改变this指向

4.11、事件修饰符

阻止单击事件继续传播
<a @click.stop="doThis"></a>
​
提交事件不再重载页面
<form @submit.prevent="onSubmit"></form>
​
修饰符可以串联,即阻止冒泡也阻止默认事件
<a @click.stop.prevent="doThat"></a>
​
只当在 event.target 是当前元素自身时触发处理函数
即事件不是从内部元素触发的
<div @click.self="doThat">...</div>
​
使用修饰符时,顺序很重要;相应的代码会以同样的顺序产生。因此,用 @click.prevent.self 会阻止所有的点击,
而 @click.self.prevent 只会阻止对元素自身的点击。

4.11、按键修饰符

只有在 `keyCode` 是 13 时调用 `vm.submit()`
<input v-on:keyup.13="submit"> ​

当点击enter 时调用 `vm.submit()` 
<input v-on:keyup.enter="submit"> ​

当点击enter或者space时  时调用 `vm.alertMe()`   
<input type="text" v-on:keyup.enter.space="alertMe"> ​ 
​
常用的按键修饰符
.enter => enter键
.tab => tab键
.delete (捕获“删除”和“退格”按键) => 删除键
.esc => 取消键
.space => 空格键
.up => 上
.down => 下
.left => 左
.right =>  右
​
<script>
    var vm = new Vue({
        el: "#app",
        methods: {
            submit: function() {},
            alertMe: function() {},
        }
    })​
</script>

自定义按键修饰符别名

<div id="app">
    预先定义了keycode 116(即F5)的别名为f5,因此在文字输入框中按下F5,会触发prompt方法
    <input type="text" v-on:keydown.f5="prompt()">
</div>
​
<script>
    Vue.config.keyCodes.f5 = 116;​
    let app = new Vue({
        el: '#app',
        methods: {
            prompt: function() {
                alert('我是 F5!');
            }
        }
    });
</script>

4.12、v-bind

<!-- 绑定一个属性 -->
<img v-bind:src="imageSrc">
​
<!-- 缩写 -->
<img :src="imageSrc">

绑定对象

1、 v-bind 中支持绑定一个对象 如果绑定的是一个对象 则 键为 对应的类名 值 为对应data中的数据
    <!-- 
    HTML最终渲染为 <ul class="box textColor textSize"></ul>
    注意:
    textColor,textSize  对应的渲染到页面上的CSS类名 
    isColor,isSize  对应vue data中的数据  如果为true 则对应的类名 渲染到页面上 
   ​
   ​
    当 isColor 和 isSize 变化时,class列表将相应的更新,
    例如,将isSize改成false,
    class列表将变为 <ul class="box textColor"></ul>
   -->
​
    <style>
        ​ .box {
            border: 1px dashed #f0f;
        }
        
        .textColor {
            color: #f00;
            background-color: #eef;
        }
        
        .textSize {
            font-size: 30px;
            font-weight: bold;
        }
    </style>

<ul class="box" v-bind:class="{textColor:isColor, textSize:isSize}">
    <li>学习Vue</li>
    <li>学习Node</li>
    <li>学习React</li>
</ul>
<div v-bind:style="{color:activeColor,fontSize:activeSize}">对象语法</div>
​
<script src="./vue.js"></script>
<script>
    var vm = new Vue({ 
        el:'.box', 
        data:{ 
            isColor:true, isSize:true,
            activeColor:"red", 
            activeSize:"25px" 
        } 
    });
</script>

绑定class

2、 v-bind 中支持绑定一个数组   数组中的 classA 对应data 中的 classA , classB 对应data 中的 classB
<ul class="box" :class="[classA, classB]">
    <li>学习Vue</li>
    <li>学习Node</li>
    <li>学习React</li>
</ul>
<script>
    var vm = new Vue({
        el: '.box',
        data: {
            classA: ‘textColor‘,
            classB: ‘textSize‘
        }
    })
</script>
<style>
    .box {
        border: 1px dashed #f0f;
    }
    
    .textColor {
        color: #f00;
        background-color: #eef;
    }
    
    .textSize {
        font-size: 30px;
        font-weight: bold;
    }
</style>

绑定对象和绑定数组 的区别

样式绑定相关语法细节:

绑定style(行内样式)

<div v-bind:style="styleObject">绑定样式对象</div>

CSS 属性名可以用驼峰式 (camelCase) 或短横线分隔 (kebab-case,记得用单引号括起来)   
<div v-bind:style="{ color: activeColor, fontSize: fontSize, background:'red' }">内联样式</div>
​
组语法可以将多个样式对象应用到同一个元素 
<div v-bind:style="[styleObj1, styleObj2]"></div>
​ ​
<script>
    new Vue({
        el: '#app',
        data: {
            styleObject: {
                color: 'green',
                fontSize: '30px',
                background: 'red'
            },
            activeColor: 'green',
            fontSize: "30px"
        },
        styleObj1: {
            color: 'red'
        },
        styleObj2: {
            fontSize: '30px'
        }​
    })
</script>

4.13、分支结构

v-if 使用场景

<div id="app">
 <!--  判断是否加载,如果为真,就加载,否则不加载-->
 <span v-if="flag">
 如果flag为true则显示,false不显示!
 </span>
</div>
​
<script>
 var vm = new Vue({
 el:"#app",
 data:{
 flag:true
 }
 })
</script>
​
----------------------------------------------------------
​
 <div v-if="type === 'A'">
 A
 </div>
 <!-- v-else-if紧跟在v-if或v-else-if之后   表示v-if条件不成立时执行-->
 <div v-else-if="type === 'B'">
 B
 </div>
 <div v-else-if="type === 'C'">
 C
 </div>
 <!-- v-else紧跟在v-if或v-else-if之后-->
 <div v-else>
 Not A/B/C
 </div>
​
<script>
 new Vue({
 el: '#app',
 data: {
 type: 'C'
 }
 })
</script>

v-show 和 v-if的区别

4.14、循环结构

v-for

<ul id="example-1">
 <!-- 循环结构-遍历数组
 item 是我们自己定义的一个名字  代表数组里面的每一项
 items对应的是 data中的数组-->
 <li v-for="item in items">
 {{ item.message }}
 </li> 
​
</ul>
<script>
 new Vue({
 el: '#example-1',
 data: {
 items: [
 { message: 'Foo' },
 { message: 'Bar' }
 ],

 }
})
</script>
 <!--  循环结构-遍历对象
 v 代表   对象的value
 k  代表对象的 键 
 i  代表索引 
 ---> 
 <div v-if='v==13' v-for='(v,k,i) in obj'>{{v + '---' + k + '---' + i}}</div>
​
<script>
 new Vue({
 el: '#example-1',
 data: {
 items: [
 { message: 'Foo' },
 { message: 'Bar' }
 ],
 obj: {
 uname: 'zhangsan',
 age: 13,
 gender: 'female'
 }
 }
})
</script>
<ul>
 <li v-for="item in items" :key="item.id">...</li>
</ul>

案例选项卡

1、 HTML 结构

`
 <div id="app">
 <div class="tab">
 <!--  tab栏  -->
 <ul>
 <li class="active">apple</li>
 <li class="">orange</li>
 <li class="">lemon</li>
 </ul>
 <!--  对应显示的图片 -->
 <div class="current"><img src="img/apple.png"></div>
 <div class=""><img src="img/orange.png"></div>
 <div class=""><img src="img/lemon.png"></div>
 </div>
 </div>
​
​
`

2、 提供的数据

 list: [{
 id: 1,
 title: 'apple',
 path: 'img/apple.png'
 }, {
 id: 2,
 title: 'orange',
 path: 'img/orange.png'
 }, {
 id: 3,
 title: 'lemon',
 path: 'img/lemon.png'
 }]

3、 把数据渲染到页面

     <div id="app">
     <div class="tab">
     <ul>
     <!--
     1、绑定key的作用 提高Vue的性能 
     2、 key 需要是唯一的标识 所以需要使用id, 也可以使用index ,
     index 也是唯一的 
     3、 item 是 数组中对应的每一项
     4、 index 是 每一项的 索引
     -->
     <li :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>
     </ul>
     <div  :key='item.id' v-for='(item, index) in list'>
     <!-- :  是 v-bind 的简写   绑定属性使用 v-bind -->
     <img :src="item.path">
     </div>
     </div>
     </div>
    <script>
     new  Vue({
     //  指定 操作元素 是 id 为app 的 
     el: '#app',
     data: {
     list: [{
     id: 1,
     title: 'apple',
     path: 'img/apple.png'
     }, {
     id: 2,
     title: 'orange',
     path: 'img/orange.png'
     }, {
     id: 3,
     title: 'lemon',
     path: 'img/lemon.png'
     }]
     }
     })
    ​
    </script>

4、 给每一个tab栏添加事件,并让选中的高亮

    <ul>
    <!-- 动态绑定class   有 active   类名高亮  无 active   不高亮-->
    <li  :class='currentIndex==index?"active":""'
    :key='item.id' v-for='(item,index) in list'
    >{{item.title}}</li>
    </ul>
    <!-- 动态绑定class   有 current  类名显示  无 current  隐藏-->
    <div :class='currentIndex==index?"current":""' 

    :key='item.id' v-for='(item, index) in list'>
    <!-- :  是 v-bind 的简写   绑定属性使用 v-bind -->
    <img :src="item.path">
    </div>
   ​
   <script>
    new  Vue({
    el: '#app',
    data: {
    currentIndex: 0, // 选项卡当前的索引  默认为 0
    list: [{
    id: 1,
    title: 'apple',
    path: 'img/apple.png'
    }, {
    id: 2,
    title: 'orange',
    path: 'img/orange.png'
    }, {
    id: 3,
    title: 'lemon',
    path: 'img/lemon.png'
    }]
    }
    })
   ​
   </script>
上一篇下一篇

猜你喜欢

热点阅读