悬停提示信息
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<!--View层,模板-->
<div id="app">
<span v-bind:title="message">鼠标悬停几秒钟查看提示信息!!!</span>
</div>
<script>
var vm = new Vue({
el: "#app",
//Model:数据
data: {
message: "hello Vue!"
}
});
</script>
</body>
</html>
判断
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.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="type==='C'">C</h1>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
type: 'A'
}
});
</script>
</body>
</html>
循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<li v-for="item in items">
{{item.message}}
</li>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
items: [
{message: 'Java'},
{message: 'Python'}
]
}
});
</script>
</body>
</html>
事件绑定
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<button v-on:click="say">点击</button>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: 'Hello Vue!!!'
},
//方法必须定义在Vue实例的methods对象中
methods:{
say: function (event) {
//this在方法里指向当前Vue实例
alert(this.message);
}
}
});
</script>
</body>
</html>
双向绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<select v-model="select">
<option value="" disabled>请选择:</option>
<option>Java</option>
<option>C++</option>
<option>Python</option>
</select>
<span>选择的值为:{{select}}</span>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
select: ''
}
});
</script>
</body>
</html>
组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<!--传递给组件中的值:props-->
<test v-for="item in items" v-bind:t="item"></test>
</div>
<script>
<!--定义一个Vue组件component-->
Vue.component("test",{
props: ['t'],
template: '<li>{{t}}</li>'
});
var vm = new Vue({
el:'#app',
data: {
items: ["Java","Python","C++"]
}
});
</script>
</body>
</html>
Axios异步通信
先创建一个json文件
{
"name": "嘵奇",
"url": "https://www.baidu.com",
"page": 1,
"isNonProfit": true,
"address": {
"street": "松岭路",
"city": "山东青岛",
"country": "中国"
},
"links": [
{
"name": "CSDN",
"url": "https://mp.csdn.net/console/article"
},
{
"name": "JD",
"url": "https://prodev.jd.com/"
}
]
}
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript"></script>
<!--解决闪烁问题-->
<style>
[v-clock]{
display: none;
}
</style>
</head>
<body>
<div id="app" v-clock>
<li>{{info.name}}</li>
<li>{{info.address.country}}</li>
<a v-bind:href="info.url">点击跳转</a>
</div>
<script>
var vm = new Vue({
el: "#app",
data(){
return{
info:{
name: null,
address: {
street: null,
city: null,
country: null
},
url: null
}
}
},
mounted(){
axios.get('../Axios异步通信/data.json').then(response=>(this.info=response.data));
}
});
</script>
</body>
</html>
计算属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<p>方法:{{currentTime1()}}</p>
<p>属性:{{currentTime2}}</p>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
message: 'hello Vue!!!'
},
methods: {
currentTime1:function () {
return Date.now();
}
},
computed: {
currentTime2: function () {
this.message;
return Date.now();
}
}
});
</script>
</body>
</html>