1、如何引用Vue
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> -->
<title>Document</title>
<!-- 开发版本 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
{{ message }}
</div>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
});
</script>
</html>
输出:Hello Vue!
出现问题:
①、运行输出{{ message }}
问题原因:Vue链接有问题,导致没有引用成功
②、拼写Vue时候没有代码提示
使用 newV 这样会有提示
2、Vue的用法
2.1 条件渲染
v-if
、v-else
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<!-- 开发版本 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1 v-if="some">这是some为true时候展示的内容</h1>
<h1 v-else>这是some为false时候展示的内容</h1>
</div>
<script>
var app = new Vue({
el:'#app',
data:{
some:false
},
});
</script>
</html>
输出:这是some为false时候展示的内容
v-else-if
(2.1.0新增)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<!-- 开发版本 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1 v-if="type === 'A'">A</h1>
<h1 v-else-if="type === 'B'">B</h1>
<h1 v-else-if="type === 'C'">C</h1>
<h1 v-else>Not ABC</h1>
</div>
<script>
var app = new Vue({
el:'#app',
data:{
type:'A'
},
});
</script>
</html>
2.2 列表渲染:v-for
v-for
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<!-- 开发版本 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<ul id="some">
<li v-for="item in items">
{{ item.message}}
</li>
</ul>
<script>
var app=new Vue({
el:'#some',
data:{
items:[
{ message:'A'},{message:'B'}
]
}
})
</script>
</html>
输出:
v-for 迭代对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<!--axios 网络请求-->
<!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="item in objets">{{item}}</li>
</ul>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
objets: {
name: "百度",
url: "http://www.baidu.com",
slogan: "学的不仅是技术,更是梦想!",
},
},
});
</script>
</body>
</html>
输出:
- 也可提供多个参数为键名:
v-for="(value, key) in object"
或者v-for="(value, key, index) in object"
两个参数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<!--axios 网络请求-->
<!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="(value, key) in objets">{{ index }}. {{ key }}</li>
</ul>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
objets: {
name: "百度",
url: "http://www.baidu.com",
slogan: "学的不仅是技术,更是梦想!",
},
},
});
</script>
</body>
</html>
输出:
三个参数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<!--axios 网络请求-->
<!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="(value, key,index) in objets">
{{ index }}. {{ key }} : {{ value }}
</li>
</ul>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
objets: {
name: "百度",
url: "http://www.baidu.com",
slogan: "学的不仅是技术,更是梦想!",
},
},
});
</script>
</body>
</html>
v-for 迭代整数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<!--axios 网络请求-->
<!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="n in 10">{{n}}</li>
</ul>
</div>
<script>
var app = new Vue({
el: "#app",
});
</script>
</body>
</html>
输出:
2.3 事件处理: v-on
v-on
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<!-- 开发版本 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="some">
<button v-on:click="greet">Greet</button>
</div>
<script>
var app=new Vue({
el:'#some',
data:{
name:'Vue.js'
},
methods: {
greet:function(event){
alert(`Hello ${this.name}`);
if(event){
alert(event.target.tagName);
}
}
},
})
</script>
</html>
v-on:
可简写为@
- 将
click
换成其他事件同样成立,比如鼠标进入事件mouseenter
。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style></style>
</head>
<body>
<div id="app">
<button @mouseenter="number--">-</button>
<span>{{ number }}</span>
<button @mouseenter="number++">+</button>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
number: 1,
},
});
</script>
</body>
</html>
2.4 表单输入绑定 :v-model
(双向绑定)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" name="name" id="myName" v-model="message" />
<p>内容为:{{message}}</p>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
message: "Hello",
},
});
</script>
</body>
</html>
v-model
结合radio
类型使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style></style>
</head>
<body>
<div id="app">
<label for="male">
<input type="radio" id="male" name="sex" value="男" v-model="sex" />男
</label>
<label for="female">
<input type="radio" id="female" name="sex" value="女" v-model="sex" />女
</label>
<div>你选择的性别为:{{ sex}}</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
sex: "男",
},
});
</script>
</body>
</html>
v-model
结合复选框类型使用:
单选:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style></style>
</head>
<body>
<div id="app">
<label for="agree">
<input
type="checkbox"
id="agree"
value="男"
v-model="isAgree"
/>是否阅读并同意用户协议
</label>
<div>你的选择为:{{ isAgree}}</div>
<button :disabled="!isAgree">下一步</button>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
isAgree: true,
},
});
</script>
</body>
</html>
输出:
多选:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style></style>
</head>
<body>
<div id="app">
<input type="checkbox" name="hobby" value="苹果" v-model="choose" />苹果
<input type="checkbox" name="hobby" value="香蕉" v-model="choose" />香蕉
<input
type="checkbox"
name="hobby"
value="车厘子"
v-model="choose"
/>车厘子
<div>你的选择为:{{ choose}}</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
choose: [],
},
});
</script>
</body>
</html>
输出:
- 值的绑定:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style></style>
</head>
<body>
<div id="app">
<label :for="item" v-for="(item, index) in hhobbies" :key="index">
<input
type="checkbox"
name="hobby"
:value="item"
v-model="hobby"
/>{{item}}
</label>
<div>你的选择为:{{ hobby}}</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
hhobbies: ["肖战", "王一博", "谢怜"],
hobby: [],
},
});
</script>
</body>
</html>
输出:
v-model
结合select
实现单选或者多选:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style></style>
</head>
<body>
<div id="app">
<!-- 单选 -->
<select name="pinPai" v-model="pinPai">
<option value="纪梵希">纪梵希</option>
<option value="YSL">YSL</option>
<option value="雅诗兰黛">雅诗兰黛</option>
</select>
<div>你的选择为:{{ pinPai }}</div>
<!-- 多选 按住ctrl键实现多选-->
<select name="pinPais" v-model="pinPais" multiple>
<option value="纪梵希">纪梵希</option>
<option value="YSL">YSL</option>
<option value="雅诗兰黛">雅诗兰黛</option>
</select>
<div>你的选择为:{{ pinPais}}</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
pinPai: "纪梵希",
pinPais: [],
},
});
</script>
</body>
</html>
输出:
2.5 计算属性:computed
computed
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<!--axios 网络请求-->
<!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<p>原始字符串为:{{message}}</p>
<p>翻转后的字符串为:{{reversedMessage}}</p>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
message: "Hello",
},
computed: {
//计算属性的getter
reversedMessage: function () {
return this.message.split("").reverse().join();
},
},
});
</script>
</body>
</html>
输出:
2.6 监听属性:watch
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<p>计数器:{{count}}</p>
<button @click="count++">点击计数</button>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
count: 1,
},
watch: {
count: function (newvalue, oldValue) {
this.count = newvalue;
// alert("原值:" + oldValue + "新值:" + newvalue);
},
},
});
</script>
</body>
</html>
2.7 样式绑定: v-bind
v-bind 指令用于动态绑定 HTML 属性和组件属性。通过 v-bind 指令,可以将 Vue 实例中的数据动态地绑定到 HTML 元素的属性上,从而实现数据的动态更新
和响应式渲染
。
2.7.1 使用v-bind指令:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<img v-bind:src="img" alt="Image" />
</div>
<script>
var app = new Vue({
el: "#app",
data: {
img: "search.png",
},
});
</script>
</body>
</html>
输出:
v-bind
语法糖(简写)
<img :src="img" alt="Image" />
2.7.2 v-bind
动态绑定cl
ass
- 基本使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<!-- <h1 :class="className">标题</h1> -->
<h1 :class="className">标题</h1>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
className: "active",
},
});
</script>
</body>
</html>
class
动态绑定 ( 对象语法 )
对象语法有下面这些用法
(1)直接通过{ }绑定一个类 : <h2 :class="{'active': isActive}">Hello World</h2>
(2)可以通过判断, 传入多个值 : <h2 :class="{'active': isActive, 'line': isLine}">Hello World</h2>
(3)和普通的类同时存在 : <h2 class="title" :class="{'active': isActive}">Hello World</h2>
对象里面的参数是以键值对形式存在的.
键为类名, 值为布尔值.
当值为true的时候, 这个类就会被添加到标签上面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<!-- <h1 :class="className">标题</h1> -->
<h1 :class="{active: isActive}">动态绑定</h1>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
isActive: false,
},
});
</script>
</body>
</html>
isActive为true时候h1的颜色为red
- 示例:点击实现字体变色:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h1 :class="{active:isActive}">标题</h1>
<button v-on:click="btnClick">点击变换颜色</button>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
isActive: true,
},
methods: {
btnClick: function () {
this.isActive = !this.isActive;
},
},
});
</script>
</body>
</html>
- 把
class
的对象提取为一个方法并调用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h1 :class="getClasses()">标题</h1>
<button v-on:click="btnClick">点击变换颜色</button>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
isActive: true,
},
methods: {
getClasses: function () {
return { active: this.isActive };
},
btnClick: function () {
this.isActive = !this.isActive;
},
},
});
</script>
</body>
</html>
class
动态绑定 ( 数组语法 ):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li
v-for="(item, index) in items"
:class="{active:index==currentIndex}"
v-on:click="setIndex(index)"
>
{{item}}
</li>
</ul>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
currentIndex: 0,
items: ["大话西游1", "大话西游2", "大话西游3"],
},
methods: {
setIndex: function (index) {
this.currentIndex = index;
},
},
});
</script>
</body>
</html>
v-bind
动态绑定style
语法:
<h2 :style="{key(属性名): value(属性值)}"></h2>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 开发版本 -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h1 :style="{'font-size': '50px'}">标题1</h1>
<h1 :style="{'font-size': size}">标题2</h1>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
size: "100px",
},
});
</script>
</body>
</html>
输出: