海投汇前端培训
简单介绍Vue,详细文档请查看Vue官方文档
1、vue是什么
Vue是一个用于构建用户界面的渐进式JavaScript框架。Vue核心库只关注视图层。
2、声明式渲染
Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统
示例代码
// 实现一个hello world
// html
<div id="app">
hello {{ name }}
</div>
// js
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
2、Vue指令
- (1)v-text
作用:更新元素的textContent
。
示例:<span v-text="msg"></span> <span>{{ msg }}</span>
- (2)v-html
作用:更新元素的innerHtml
示例:<div v-html="html"></div>
- (3)v-show
作用:根据表达式之真假值,切换元素的display
CSS 属性。
示例:<div v-show="show"></div>
- (4)v-if
作用:根据表达式的值的真假条件渲染元素。在切换时元素及它的数据绑定 / 组件被销毁并重建。如果元素是 <template> ,将提出它的内容作为条件块。
示例:<div v-if="true">hello</div>
- (5)v-else
示例:<div v-if="type === 'a'">a</div> <div v-else-if="type === 'b'">b</div> <div v-else>not a/b</div>
- (6)v-else-if
示例:<div v-if="type === 'a'">a</div> <div v-else>not a</div>
- (7)v-for
作用:基于源数据多次渲染元素或模板块。
示例:<div v-for="item in items"> {{ item.text }} </div> <div v-for="(item, index) in items" :key="item.id"></div> <div v-for="(val, key) in object"></div> <div v-for="(val, key, index) in object"></div>
- (8)v-on
用法:绑定事件监听器。缩写为@
示例:<button v-on:click="click"></button> <!-- 内联语句 --> <button v-on:click="doThat('hello', $event)"></button> <!-- 缩写 --> <button @click="click"></button> <!-- 停止冒泡 --> <button @click.stop="doThis"></button> <!-- 阻止默认行为 --> <button @click.prevent="doThis"></button> <!-- 阻止默认行为,没有表达式 --> <form @submit.prevent></form> <!-- 键修饰符,键别名 --> <input @keyup.enter="onEnter"> <!-- 键修饰符,键代码 --> <input @keyup.13="onEnter"> <!-- 点击回调只会触发一次 --> <button @click.once="doThis"></button>
- (9)v-bind
用法:动态地绑定一个或多个特性,或一个组件 prop 到表达式。
示例:<!-- 绑定一个属性 --> <img v-bind:src="imageSrc"> <!-- 缩写 --> <img :src="imageSrc"> <!-- 内联字符串拼接 --> <img :src="'/path/to/images/' + fileName"> <!-- class 绑定 --> <div :class="{ red: isRed }"></div> <div :class="[classA, classB]"></div> <div :class="[classA, { classB: isB, classC: isC }]"> <!-- style 绑定 --> <div :style="{ fontSize: size + 'px' }"></div> <div :style="[styleObjectA, styleObjectB]"></div> <!-- 绑定一个有属性的对象 --> <div v-bind="{ id: someProp, 'other-attr': otherProp }"></div> <!-- 通过 prop 修饰符绑定 DOM 属性 --> <div v-bind:text-content.prop="text"></div> <!-- prop 绑定。“prop”必须在 my-component 中声明。--> <my-component :prop="someThing"></my-component> <!-- 通过 $props 将父组件的 props 一起传给子组件 --> <child-component v-bind="$props"></child-component> <!-- XLink --> <svg><a :xlink:special="foo"></a></svg>
- (10)v-model
用法:在表单控件或者组件上创建双向绑定
示例:<input type="text" v-model="username"></input> // 输入字符串转为数字 <input type="text" v-model.numer="username"></input> // 输入首尾空格过滤 <input type="text" v-model.trim="username"></input>
- (11)v-pre
用法:跳过这个元素和它的子元素的编译过程。可以用来显示原始 Mustache 标签。
示例:<span v-pre>{{ this will not be compiled }}</span>
- (12)v-cloak
用法:这个指令保持在元素上直到关联实例结束编译。和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Mustache 标签直到实例准备完毕。
示例:// css [v-cloak] { display: none; } // html <div v-cloak> {{ message }} </div>
- (13)v-once
用法:只渲染元素和组件一次。随后的重新渲染,元素/组件及其所有的子节点将被视为静态内容并跳过。这可以用于优化更新性能。
示例:<!-- 单个元素 --> <span v-once>This will never change: {{msg}}</span> <!-- 有子元素 --> <div v-once> <h1>comment</h1> <p>{{msg}}</p> </div> <!-- 组件 --> <my-component v-once :comment="msg"></my-component> <!-- `v-for` 指令--> <ul> <li v-for="i in list" v-once>{{i}}</li>
</ul>