简介
Vue 是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。另一方面,当与现代化的工具链以及各种支持类库结合使用时,Vue 也完全能够为复杂的单页应用提供驱动。
1、安装vue
方式一:直接使用script标签引入
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
方式二: npm安装
在用 Vue 构建大型应用时,推荐使用 NPM 安装
npm install vue
2、vue 指令和插值
1.{{ value}}:插值表达式,绑定数据,语法如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<ul>
<li>姓名: {{name}}</li>
<li>年龄: {{age}}</li>
</ul>
</div>
<script type="text/javascript">
// 创建vue实例
var vm = new Vue({
// el:指定vue实例所绑定的根节点
el: '#work',
//data:定义vue实例中的数据(页面上显示的数据)
data: {
name:'test',
age:18
}
})
</script>
</body>
</html>
2.v-text:填充纯文本内容,类似于js中的innerText
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<h1 v-text="name"></h1>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
name:'test',
age:18
}
})
</script>
</body>
</html>
3.v-html:填充html,类似于js中的innerHTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<h1 v-html="info"></h1>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
name:'test',
age:18,
info:'<h1>test</h1>'
}
})
</script>
</body>
</html>
4.v-pre:显示原始数据,不会解析为插值表达式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<ul>
<li v-pre>姓名:{{name}}</li>
<li v-pre>年龄: {{age}}</li>
</ul>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
name:'test',
age:18,
info:'<h1>test</h1>'
}
})
</script>
</body>
</html>
3、属性绑定
属性绑定:使用v-bind:属性 也可以简写为 :属性
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<a v-bind:href="url1.url">{{url1.title}}</a>
<a :href="url2.url">{{url2.title}}</a> // 简写为:
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
url1:{
url:'www.baidu.com',
title:'百度'
},
url2:{
url:'www.taobao.com',
title:'淘宝'
}
}
})
</script>
</body>
</html>
4、事件绑定
v-on:为元素绑定一个事件,绑定点击事件。
v-on:click = ‘指定执行的方法’,可简写为@click
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<button type="button" v-on:click="test()">弹窗</button>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
name: 'test'
},
<!-- 方法定义在methods{}中 -->
methods:{
test() {
alert('666')
}
}
})
</script>
</body>
</html>
5、vue的双向数据绑定
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<input type="text" :value="nu1"><br>
<input type="text" :value="nu2"><br>
<button type="button" @click="add()">相加</button>
<h3>结果:{{res}}</h3>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
nu1:11,
nu2:22,
res:null
},
methods:{
add(){
this.res = this.nu1+this.nu2
}
}
})
</script>
</body>
</html>
当修改页面中的元素时,vue实例中的data数据不会修改,这时数据为单向形式:
v-model:页面修改数据会变,数据改变,页面也会改,如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<input type="text" v-model="nu1"><br>
<input type="text" v-model="nu2"><br>
<button type="button" @click="add()">相加</button>
<h3>结果:{{res}}</h3>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
nu1:11,
nu2:22,
res:null
},
methods:{
add(){
this.res = this.nu1+this.nu2
}
}
})
</script>
</body>
</html>
而此时input输入框,输入后默认为字符串的格式:
需要用到修饰符来解决,vue官方文档描述如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<input type="text" v-model.number="nu1"><br>
<input type="text" v-model.number="nu2"><br>
<button type="button" @click="add()">相加</button>
<h3>结果:{{res}}</h3>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
nu1:11,
nu2:22,
res:null
},
methods:{
add(){
this.res = this.nu1+this.nu2
}
}
})
</script>
</body>
</html>
v-model.lazy与v-model.trim就不一一列举了,也可以通过类似链式调用的方式组合使用:
v-model.lazy.number.trim
6、分支语句
v-if:判断条件成立时显示,
举例:当li数组中的第一个元素为11时,,li显示为蓝色
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<h3 v-if="li[0]==11" style="color: #0000FF;">{{li}}</h3>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
li:[11,22,33,44]
}
})
</script>
</body>
</html>
v-else-if:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<h3 v-if="li[0]==22" style="color: #0000FF;">{{li}}</h3>
<h3 v-else-if="li[0]==11" style="color: #ff0000;">{{li}}</h3>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
li:[11,22,33,44]
}
})
</script>
</body>
</html>
v-else:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<h3 v-if="li[0]==22" style="color: #0000FF;">{{li}}</h3>
<h3 v-else-if="li[0]==33" style="color: #ff0000;">{{li}}</h3>
<h3 v-else>不符合条件</h3>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
li:[11,22,33,44]
}
})
</script>
</body>
</html>
7、遍历语句
v-for:遍历数组
举例:遍历三次后会生成三个包含内容的div标签,item为遍历后的值
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<div v-for="item in info">{{item}}</div>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
info: [{
name: 'test',
age: 11
},
{
name: 'test01',
age: 12
},
{
name: 'test02',
age: 11
},
]
}
})
</script>
</body>
</html>
v-for集合v-if一起使用:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<div v-for="item in info">
<h3 v-if="item.age==11" style="color: red;">{{item}}</h3>
<h3 v-else-if="item.age==12" style="color: blue;">{{item}}</h3>
<h3 v-else style="color: green;">{{item}}</h3>
</div>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
info: [{
name: 'test',
age: 11
},
{
name: 'test01',
age: 12
},
{
name: 'test02',
age: 13
},
]
}
})
</script>
</body>
</html>
遍历对象:
v-for="value,keyin array"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="work">
<div v-for="value,key in info">键:{{key}},值:{{value}}</div>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#work',
data: {
info:{name:'test',age:18}
}
})
</script>
</body>
</html>