What is Vue?
- Vue (pronounced /vjuː/, similar to view ) is a progressive framework for building user interfaces
- The core library of vue only focuses on the view layer, which is not only easy to use, but also easy to integrate with third-party libraries or existing projects
Use Vue to render helloworld on the page
instruction
- The essence is custom attributes
- The designations in Vue all start with v-
v-cloak
- Prevent flickering when the page loads
<style type="text/css">
/*
1、通过属性选择器 选择到 带有属性 v-cloak的标签 让他隐藏
*/
[v-cloak]{
/* 元素隐藏 */
display: none;
}
</style>
<body>
<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>
</body>
</html>
v-text
- The v-text command is used to fill the data into the label, which is similar to the interpolation expression, but there is no flickering problem
- If there are HTML tags in the data, the html tags will be output together
- Note: This is a one-way binding, the value of the data object changes, the interpolation will change; but when the interpolation changes, it will not affect the value of the data object
<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>
v-html
- Usage is similar to v-text but it can fill HTML fragments into tags
- There may be security issues, generally only use
v-html
trusted content, never for user-submitted content - The difference between it and v-text is that the output of v-text is plain text, and the browser will not parse it again by html, but v-html will output it as html tags after parsing.
<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>
v-pre
- Display original information and skip the compilation process
- Skip the compilation process of this element and its child elements.
- Some static content does not need to be compiled plus this instruction can speed up rendering
<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-once
- Perform a one-time interpolation [When the data changes, the content of the interpolation will not continue to update]
<!-- 即使data里面定义了msg 后期我们修改了 仍然显示的是第一次data里面存储的数据即 Hello Vue.js -->
<span v-once>{{ msg}}</span>
<script>
new Vue({
el: '#app',
data: {
msg: 'Hello Vue.js'
}
});
</script>
Two-way data binding
- When the data changes, the view also changes
- When the view changes, the data will also change synchronously
v-model
- v-model is an instruction, restricted to be used
<input>、<select>、<textarea>、components
<div id="app">
<div>{{msg}}</div>
<div>
当输入框中内容改变的时候, 页面上的msg 会自动更新
<input type="text" v-model='msg'>
</div>
</div>
mvvm
- MVC is the concept of hierarchical development of the back-end; MVVM is the concept of the front-end view layer, which mainly focuses on the separation of the view layer, that is to say: MVVM divides the front-end view layer into three parts: Model, View, VM ViewModel
m model
- Data layer The data layer in Vue is placed in data
v view
- View in Vue is our HTML page
vm (view-model) The controller connects the data with the view layer
- vm means Vue instance is vm
v-on
- Used to bind events
- Such as: v-on:click abbreviated as @click;
Incoming parameters in the v-on event function
<body>
<div id="app">
<div>{{num}}</div>
<div>
<!-- 如果事件直接绑定函数名称,那么默认会传递事件对象作为事件函数的第一个参数 -->
<button v-on:click='handle1'>点击1</button>
<!-- 2、如果事件绑定函数调用,那么事件对象必须作为最后一个参数显示传递,
并且事件对象的名称必须是$event
-->
<button v-on: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) {
console.log(event.target.innerHTML)
},
handle2: function(p, p1, event) {
console.log(p, p1)
console.log(event.target.innerHTML)
this.num++;
}
}
});
</script>
Event modifier
event.stopPropagation()
is a very common requirementevent.preventDefault()
or 06077037bebf6f in the event handler.- Vue does not recommend us to manipulate the DOM. To solve this problem,
v-on
provides event modifier - Modifiers are represented by the instruction suffix at the beginning of the dot
<!-- 阻止单击事件继续传播 -->
<a v-on:click.stop="doThis"></a>
<!-- 提交事件不再重载页面 -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- 修饰符可以串联 即阻止冒泡也阻止默认事件 -->
<a v-on:click.stop.prevent="doThat"></a>
<!-- 只当在 event.target 是当前元素自身时触发处理函数 -->
<!-- 即事件不是从内部元素触发的 -->
<div v-on:click.self="doThat">...</div>
使用修饰符时,顺序很重要;相应的代码会以同样的顺序产生。因此,用 v-on:click.prevent.self 会阻止所有的点击,而 v-on:click.self.prevent 只会阻止对元素自身的点击。
Key modifier
- Keyboard events are sometimes used in projects. When listening to keyboard events, we often need to check the detailed keystrokes. Vue allows
v-on
to add key modifiers when listening to keyboard events
<!-- 只有在 `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>
Custom key modifier alias
- In Vue, you can customize key modifier aliases
config.keyCodes
<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>
v-bind
- The v-bind command is used to update HTML attributes responsively
- v-bind: href can be abbreviated as: href;
<!-- 绑定一个属性 -->
<img v-bind:src="imageSrc">
<!-- 缩写 -->
<img :src="imageSrc">
Binding object
- We can give v-bind:class an object to dynamically switch classes.
- Note: The v-bind:class instruction can coexist with ordinary class features
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>
-->
<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>
<sript>
var vm= new Vue({
el:'.box',
data:{
isColor:true,
isSize:true,
activeColor:"red",
activeSize:"25px",
}
})
</sript>
<style>
.box{
border:1px dashed #f0f;
}
.textColor{
color:#f00;
background-color:#eef;
}
.textSize{
font-size:30px;
font-weight:bold;
}
</style>
Binding class
2、 v-bind 中支持绑定一个数组 数组中classA和 classB 对应为data中的数据
这里的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>
The difference between bound object and bound array
- When binding the object, the attribute of the object is the attribute value of the class name object to be rendered corresponding to the data in data
- When binding the array, the data in the array is stored in the data
Binding 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>
Branch structure
v-if usage scenarios
- 1- Multiple elements show or hide an element through conditional judgment. Or multiple elements
- 2- Switch between the two views
<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>
The difference between v-show and v-if
The essence of v-show is that the label display is set to none to control hiding
- v-show only compiles once, and then it actually controls css, while v-if keeps destroying and creating, so the performance of v-show is better.
v-if is to dynamically add or delete DOM elements to the DOM tree
- The v-if switch has a partial compilation/unloading process. During the switch process, the internal event listeners and sub-components are appropriately destroyed and rebuilt
Cyclic structure
v-for
- The values in the array used for looping can be objects or ordinary elements
<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>
- not recommended use
v-if
andv-for
- When
v-if
isv-for
togetherv-for
has a higher priorityv-if
<!-- 循环结构-遍历对象
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>
The role of key
- key to uniquely identify each node
- key is mainly used to efficiently update the virtual DOM
<ul>
<li v-for="item in items" :key="item.id">...</li>
</ul>
Case tab
1. HTML structure
`
<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. Data provided
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. Render the data to the page
Replace the number in the tab bar on the page
- Use the v-for loop to render the title in the data onto the page
- Use the v-for loop to render the path in the data onto the page
<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. Add events to each tab bar and make the selected highlight
4.1, make the default first tab bar highlighted
Tab bar highlighting is achieved by adding the class name active (CSS active style has been written in advance)
- Define a default index currentIndex in data as 0
- Add to the first li
active class name
- By dynamically binding the class to realize that the index of the first li is 0 and the value of currentIndex is exactly equal
- currentIndex === index If equal, add the class name active, otherwise add an empty class name
4.2. Let the default div corresponding to the first tab bar display
- The realization idea is the same as the first tab realization idea, except that the class name that controls the first div display is current
<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>
4.3. Click the current highlight of each tab bar and the other to cancel the highlight
- Add a click event to each li
- Compare the current index index with the current index value
- If they are equal, the current li adds the active class name, the current li highlights the div corresponding to the current index, and the current current div shows other hidden
<div id="app">
<div class="tab">
<ul>
<!-- 通过v-on 添加点击事件 需要把当前li 的索引传过去
-->
<li v-on:click='change(index)'
:class='currentIndex==index?"active":""'
:key='item.id'
v-for='(item,index) in list'>{{item.title}}</li>
</ul>
<div :class='currentIndex==index?"current":""'
:key='item.id' v-for='(item, index) in list'>
<img :src="item.path">
</div>
</div>
</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'
}]
},
methods: {
change: function(index) {
// 通过传入过来的索引来让当前的 currentIndex 和点击的index 值 相等
// 从而实现 控制类名
this.currentIndex = index;
}
}
})
</script>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。