模板语法
Vue.js的模板是合法的HTML
基本语法
Mustache
语法
(双大括号) 的文本插值
<span>Message: {{ msg }}</span>
无论何时,绑定的数据对象上
msg
property 发生了改变,插值处的内容都会更新。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h5>{{message}}</h5>
<h5>{{message}}, dg</h5>
<!--mustache语法中,不仅仅可以直接写变量,也可以写简单的表达式-->
<h5>{{firstName + lastName}}</h5>
<h5>{{firstName + ' ' + lastName}}</h5>
<h5>{{firstName}} {{lastName}}</h5>
<h5>{{counter * 2}}</h5>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
firstName: 'kobe',
lastName: 'bryant',
counter: 100
},
})
</script>
</body>
</html>
v-cloak
v-cloak
指令
先通过样式隐藏内容,然后在内存中进行值的替换,替换好之后再显示最终的结果
<style>
[v-cloak]{
display:none;
}
</style>
<div v-cloak>{{msg}}</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
[v-cloak]{
display:none;
}
</style>
</head>
<body>
<div id="app">
<div v-cloak>{{msg}}</div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
msg:'Hello Vue'
}
})
</script>
</body>
</html>
v-once
v-once
指令
该指令后面不需要跟任何表达式(比如之前的v-for后面是由跟表达式的)
该指令表示元素和组件(组件后面才会学习)只渲染一次,不会随着数据的改变而改变
<span v-once>这个将不会改变: {{ msg }}</span>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2>{{message}}</h2>
<h2 v-once>{{message}}</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '111'
}
})
</script>
</body>
</html>
数据绑定
v-html
指令
该指令后面往往会跟上一个string类型
会将string的html解析出来并且进行渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h4>{{url}}</h4>
<h4 v-html="url"></h4>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
url: '<a href="http://www.baidu.com">百度一下</a>'
}
})
</script>
</body>
</html>
不要对用户提供内容使用插值
v-text
指令
填充纯文本
<div v-text='msg'></div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
<div v-text='msg'></div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
msg:'Hello Vue'
}
})
</script>
</body>
</html>
v-pre
指令
显示原始信息,跳过编译过程(分析编译过程)
<div v-pre="msg">{{msg}}</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
<div v-pre="msg">{{msg}}</div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
msg:'Hello Vue',
}
})
</script>
</body>
</html>
双向数据绑定
v-model
指令
在表单控件或者组件上创建双向绑定(数据变化和页面中的变化)
<input type='text' v-model='uname'/>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
<div>
<input type="text" v-model="msg">
</div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
/*
*/
var vm = new Vue({
el:'#app',
data:{
msg:'Hello Vue'
}
})
</script>
</body>
</html>
补充
MVVM设计思想
- M(model)
- V(view)
- VM(View-Model)
事件绑定
v-on
指令
<input type='button' v-on:click='num++/'>
<input type='button' @click='num++'/>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
<div>
<button v-on:click="msg++">v-on:</button>
<button @click="msg++">@</button>
<button @click="handle">@+函数名称</button>
<button @click="handle()">@+调用函数</button>
</div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
/*
*/
var vm = new Vue({
el:'#app',
data:{
msg:0
},
methods:{
handle:function () {
//这里的this是Vue的实例对象
this.msg++
}
}
})
</script>
</body>
</html>
基本用法
- 如果事件直接绑定函数名称,那么默认会传递事件对象作为事件函数的第一个参数
- 如果事件绑定函数调用,那么事件对象必须作为最后一个参数显示传递,并且事件对象的名称必须是$event
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
<div>
<button v-on:click="handle1">v-on:</button>
<button @click="handle2(123,456,$event)">@</button>
</div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
/*
事件绑定-参数传递
1、如果事件直接绑定函数名称,那么默认会传递事件对象作为事件函数的第一个参数
2、如果事件绑定函数调用,那么事件对象必须作为最后一个参数显示传递,并且事件对象的名称必须是$event
*/
var vm = new Vue({
el:'#app',
data:{
msg:0
},
methods:{
handle2:function (p,p1,event) {
console.log(p,p1)
console. log (event.target.tagName)
//这里的this是Vue的实例对象
this.msg++
},
handle1:function (event) {
}
}
})
</script>
</body>
</html>
-
事件修饰符
- .stop` 阻止冒泡
<a v-on:click.stop="handle">跳转</a>
.prevent
阻止默认行为
<a v-on:click.prevent="handle">跳转</ a>
.capture
添加事件监听器时使用事件捕获模式
<div v-on:click.capture="doThis">...</div>
.self
事件不是从内部元素触发的
<div v-on:click.self="doThat">...</div>
.once
.passive
使用修饰符时,顺序很重要;相应的代码会以同样的顺序产生。因此,用
v-on:click.prevent.self
会阻止所有的点击,而v-on:click.self.prevent
只会阻止对元素自身的点击。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
<div v-on:click="handle1">
<button v-on:click.stop="handle2">点击</button>
</div>
<div>
<a href="http:www.baidu.com" v-on:click.prevent="handle2">百度</a>
</div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
msg:0
},
methods:{
handle0:function () {
this.msg++;
},
handle1:function (event) {
//阻止冒泡
event.stopPropagation()
},
handle2:function (event) {
//阻止默认行为
event.preventDefault()
}
}
})
</script>
</body>
</html>
-
按键修饰符
.enter
回车键
<input v-on:keyup.enter='submit'>
.delete
删除键
<input v-on:keyup.delete='handle'>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<form action="">
<div>
用户名:
<input type="text" v-on:keyup.delete="clearContent" v-model="uname">
</div>
<div>
密码:
<input type="text" v-on:keyup.enter="handleSubmit" v-model="pwd">
</div>
<div>
<input type="button" v-on:click="handleSubmit" value="提交">
</div>
</form>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
uname:'',
pwd:''
},
methods:{
clearContent:function(){
//按delete键的时候,清空用户名
this.uname=''
},
handleSubmit:function () {
console.log(this.uname,this.pwd);
}
}
})
</script>
</body>
</html>
-
自定义按键修饰符
- 全局config.keyCodes对象
vue.config.keycodes.f1 = 112
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<input type="text" v-on:keyup.aaa="handle" v-model="info">
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
/*
规则:自定义按键修饰符名字是自定义的,但是对应的值必须是按键对应event.keyCode值
*/
Vue.config.keyCode.aaa=65
var vm = new Vue({
el:'#app',
data:{
info:''
},
methods:{
handle:function (event) {
console.log(event.keyCode);
}
}
})
</script>
</body>
</html>
补充
案例 - 简单计算器
实现简单的加法计算,分别输入数值a和数值b,点击计算按钮,结果显示在下面。
- 通过
v-model
指令实现数值a和数值b的绑定 - 给计算按钮绑定事件,实现计算逻辑
- 将计算结果绑定到对应位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h1>简单计算器</h1>
<div>
<span>数值A:</span>
<span>
<input type="text" v-model="a">
</span>
</div>
<div>
<span>数值B:</span>
<span>
<input type="text" v-model="b">
</span>
</div>
<div>
<button v-on:click="c">计算</button>
</div>
<div>
<span>计算结果:</span>
<span v-text="result"></span>
</div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
a:'',
b:'',
result:''
},
methods:{
c:function (event) {
//实现计算逻辑
this.result=parseInt(this.a)+parseInt(this.b)
}
}
})
</script>
</body>
</html>
属性绑定
v-bind
指令
动态处理属性
<a v-bind:href='url'>跳转</a>
<a :href='url'>跳转</a>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<a v-bind:href="url">百度</a>
<button v-on:click="a">切换</button>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
url:'http://www.baidu.com'
},
methods:{
a:function(){
this.url='https://blog.csdn.net/DeathDomain'
}
}
})
</script>
</body>
</html>
样式绑定
-
class
样式绑定- 对象语法
<div v-bind:class="{ active: isActive }"></div>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> .active{ border: 1px solid red; width: 100px; height: 100px; } .error{ background-color: orange; } </style> </head> <body> <div id="app"> <div v-bind:class="{active:isActive,error:isError}">测试</div> <button v-on:click="a">切换</button> </div> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script type="text/javascript"> var vm = new Vue({ el:'#app', data:{ isActive:true, isError:true }, methods: { a:function () { //控制isActive的值在true和false之间进行切换 this.isActive = !this.isActive this.isError = !this.isError } } }) </script> </body> </html>
- 数组语法
<div v-bind:class=" [activeclass, errorClass] "></div>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> .active{ border: 1px solid red; width: 100px; height: 100px; } .error{ background-color: orange; } </style> </head> <body> <div id="app"> <div v-bind:class="[activeClass,errorClass]">测试</div> <button v-on:click="a">切换</button> </div> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script type="text/javascript"> var vm = new Vue({ el:'#app', data:{ activeClass:'active', errorClass:'error' }, methods: { a:function () { this.activeClass='' this.errorClass='' } } }) </script> </body> </html>
-
style样式处理
- 对象语法
<div v-bind:style="{ color: activeColor, fontsize: fontsize } "></div>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <div v-bind:style="{border:borderStyle,width:widthStyle,height:heightStyle}"></div> <div v-bind:style="objStyle"></div> <button v-on:click="a">切换</button> </div> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script type="text/javascript"> var vm = new Vue({ el:'#app', data:{ borderStyle:'1px solid blue', widthStyle:'100px', heightStyle:'100px', objStyle:{ border:"1px solid green", width:"200px", height:'200px' } }, methods:{ a:function () { this.heightStyle = '200px' this.objStyle.width='100px' } } }) </script> </body> </html>
- 数组语法
<div v-bind:style="[baseStyles,overridingStyles]"</div>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <div v-bind:style="{border:borderStyle,width:widthStyle,height:heightStyle}"></div> <div v-bind:style="objStyle"></div> <div v-bind:style='[objStyle, overrideStyles]'></div> <button v-on:click="a">切换</button> </div> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script type="text/javascript"> var vm = new Vue({ el:'#app', data:{ borderStyle:'1px solid blue', widthStyle:'100px', heightStyle:'100px', objStyle:{ border:"1px solid green", width:"200px", height:'200px' }, overrideStyles:{ border: '5px solid red', backgroundColor:'blue' } }, methods:{ a:function () { this.heightStyle = '200px' this.objStyle.width='100px' } } }) </script> </body> </html>
细节处理
样式绑定相关语法细节:
- 对象绑定和数组绑定可以结合使用
- class绑定的值可以简化操作
- 默认的class如何处理?默认的class会保留
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
.active{
border: 1px solid red;
width: 100px;
height: 100px;
}
.error{
background-color: orange;
}
.test{
color: black;
}
.base{
font-size: 10px;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="[activeClass,errorClass,{test:isTest}]">测试</div>
<div v-on:click="arrClasses"></div>
<div v-on:click="objClasses"></div>
<div class="base" v-bind:class="objClasses"></div>
<button v-on:click="a">切换</button>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:'#app',
data:{
activeClass:'active',
errorClass:'error',
isTest:true,
arrClasses:['active','error'],
objClasses:{
active:true,
error:true
}
},
methods: {
a:function () {
// this.isTest = false
this.objClasses.error = false
}
}
})
</script>
</body>
</html>
分支循环结构
-
分支结构
- v-if
- v-else
- v-else-if
- v-show
-
v-if
与v-show
的区别v-if
控制元素是否渲染到页面v-show
控制元素是否显示(已经渲染到了页面)
-
循环结构
v-for
遍历数组
<li v-for='item in list'>{{item}}</ li> <li v-for='(item, index) in list'>{{iten}} + '---' +{ {index}}</li>
- key的作用:帮助Vue区分不同的元素,从而提高性能
<li :key='item.id' v-for='(item,index) in list'>{{item}} + '---'{{index}}</li>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <div>水果列表</div> <ul> <li v-for="item in fruits">{{item}}</li> <li v-for="(item,index) in fruits">{{item+'---'+index}}</li> <li v-for="item in M"> <span>{{item.ename}}</span> <span>{{item.cname}}</span> </li> </ul> </div> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script type="text/javascript"> var vm = new Vue({ el:'#app', data:{ fruits:['apple','orange','banana'], M:[{ ename:'apple', cname:'苹果' },{ ename:'orange', cname:'橘子' },{ ename:'banana', cname:'香蕉' }] }, methods:{ } }) </script> </body> </html>