1. Vue
1. 第一个Vue程序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
{{msg}}
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el:"#app",
data:{
msg:"hello,vue!"
}
});
</script>
2. v-bind绑定
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<span v-bind:title="msg">
鼠标悬停几秒可以查看动态绑定的信息
</span>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: "hello,vue!"
}
})
</script>
【说明】v-bind绑定:将元素节点的title特性和Vue实例的msg属性保持一致
3. v-if,v-else,v-else-if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h1 v-if="ok">Yes</h1>
<h1 v-else>No</h1>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
ok: true
}
});
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h1 v-if="score >= 85 && score <= 100">优秀</h1>
<h1 v-else-if="score >= 60 && score < 85">中等</h1>
<h1 v-else-if="score >= 30 && score < 60">不及格</h1>
<h1 v-else="score >= 0 && score < 30">差</h1>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
score: 77
}
});
</script>
4. v-for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<li v-for="(item,index) in items">
{{item.msg}}=={{index}}
</li>
<!--
华为==0
苹果==1
诺基亚==2
-->
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
items: [
{msg: "华为"},
{msg: "苹果"},
{msg: "诺基亚"}
]
}
});
</script>
5. v-on:绑定事件
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<button v-on:click="sayHello">Click me</button>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: "hello vue~"
},
methods: { //方法必须定义在vue的methods对象中
sayHello: function () {
alert(this.msg);
}
}
})
</script>
6. v-model:双向数据绑定
【解释】双向数据绑定:数据发生变化时,视图也发生变化;视图发生变化时,数据也发生变化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
输入的文本:<input type="text" v-model="msg">
<br>
{{msg}}
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: ""
}
});
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
性别
<input type="radio" value="男" v-model="sex">男
<input type="radio" value="女" v-model="sex">女
<p>
选中了==>{{sex}}
</p>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
sex: ""
}
});
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
下拉框
<select v-model="phone">
<option value="" disabled>--请选择--</option>
<option>苹果</option>
<option>华为</option>
<option>诺基亚</option>
</select>
<p>
手机品牌:{{phone}}
</p>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
phone: ""
}
});
</script>
2. Vue组件
1. 使用Vue.component()方法注册组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<nelws></nelws>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
/*
注意:
1.Vue.component必须写在var vm = new Vue前面
2.第一个"nelws"代表组件的名称
*/
Vue.component("nelws",{
template: '<h1>Hello</h1>'
});
var vm = new Vue({
el: "#app"
});
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<!--组件:传递给组件中的值:props-->
<nelws v-for="item in items" v-bind:nel="item"></nelws>
<!--
Java
C++
Python
Go
-->
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
Vue.component("nelws",{
props: ['nel'],
template:'<li>{{nel}}</li>'
});
var vm = new Vue({
el: "#app",
data: {
items: ["Java","C++","Python","Go"]
}
});
</script>
3. axios
1. 简单的axios
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
<script>
var vm = new Vue({
el: "#app",
mounted(){ //钩子函数
axios.get("data.json").then(response=>(console.log(response.data)));
}
})
</script>
2. 复杂一点的axios
{
"name":"nelws",
"url": "http://baidu.com",
"page": "1",
"isNonProfit":"true",
"address": {
"street": "长安区",
"city":"陕西西安",
"country": "中国"
},
"links": [
{
"name": "B站",
"url": "https://www.bilibili.com/"
},
{
"name": "4399",
"url": "https://www.4399.com/"
},
{
"name": "百度",
"url": "https://www.baidu.com/"
}
]
}
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{info.name}}</div>
<div>{{info.address.street}}</div>
<div v-for="link in info.links">
{{link.name}}=={{link.url}}
</div>
<a v-bind:href="info.url">Click me!</a>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
<script>
var vm = new Vue({
el: "#app",
//data:属性
data(){
return{
//请求的返回参数格式,必须和json字符串的一样
info:{
name: null,
address: {
street: null,
city: null,
country: null
},
url: null,
links: {
name: null,
url: null
}
}
}
},
mounted(){ //钩子函数
axios.get("data.json").then(response=>(console.log(this.info=response.data)));
}
})
</script>
4. 计算属性
1. 概念:计算属性重点突出属性,首先他是个属性,其次有计算能力(计算是动词),这里的计算就是个函数,在调用方法时,每次都需要计算,产生系统开销。不经常变化的可以使用计算属性,计算属性主要就是将不经常发生变化的结果进行缓存,节省系统开销
2. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<!--currentTime1():方法!-->
<p>Time1 {{currentTime1()}}</p>
<!--currentTime2:属性!-->
<p>Time2 {{currentTime2}}</p>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: "hello,vue~"
},
methods: {
currentTime1: function () {
return Date.now();
}
},
computed: {
currentTime2: function () {
return Date.now();
}
}
});
</script>
5. 插槽slot
1. 简单使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<todo>
<todo-title slot="todo-title" v-bind:title="title"></todo-title>
<todo-items slot="todo-items" v-for="item in items" v-bind:item="item"></todo-items>
</todo>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
Vue.component("todo",{
template: '<div>\
<slot name="todo-title"></slot>\
<ul>\
<slot name="todo-items"></slot>\
</ul>\
</div>'
});
Vue.component("todo-title",{
props: ['title'],
template: '<div>{{title}}</div>>'
});
Vue.component("todo-items",{
props: ['item'],
template: '<li>{{item}}</li>'
});
var vm = new Vue({
el: "#app",
data: {
title: "编程语言",
items: ['Java','C++','Python','Go']
}
});
</script>
2. 自定义事件分发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<todo>
<todo-title slot="todo-title" v-bind:title="title"></todo-title>
<todo-items slot="todo-items" v-for="(item,index) in items" v-bind:item="item" v-bind:index="index" v-on:remove="removeItem(index)" v-bind:key="index"></todo-items>
</todo>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
Vue.component("todo",{
template: '<div>\
<slot name="todo-title"></slot>\
<ul>\
<slot name="todo-items"></slot>\
</ul>\
</div>'
});
Vue.component("todo-title",{
props: ['title'],
template: '<div>{{title}}</div>>'
});
Vue.component("todo-items",{
props: ['item','index'],
template: '<li>{{index}}--{{item}} <button v-on:click="remove">删除</button></li>',
methods: {
remove: function (index) {
//this.$emit 自定义事件分发
this.$emit('remove',index);
}
}
});
var vm = new Vue({
el: "#app",
data: {
title: "编程语言",
items: ['Java','C++','Python','Go']
},
methods: {
removeItem: function (index) {
console.log("删除了" + this.items[index] + "OK");
this.items.splice(index,1); //一次删除一个元素
}
}
});
</script>