●变量拼接
var num = 100;
var flag = true;
var str = "这是一行" + num + "字符" + flag + "串";
//${变量}
var s = `这是一行${num}字符${flag}串`;
●JSON对象键值对写法
var username = "jack";
var password = "123456";
var json = {
"username": username,
"password": password
}
//键的名字和值的变量名称相同 可以简写如下
var json2 = {
username,
password
};
●JSON对象 键的值是一个函数时
var json3 = {
"username": username,
"password": password,
eat: function() {
alert("eat");
}
}
json3.eat();
//es6中 JSON对象 键的值 是一个函数 可以简写如下
var json4 = {
"username": username,
"password": password,
eat() {
alert("eat");
}
}
json4.eat();
●形参默认值
function show(a = 100, b = 200) {
alert(a + "====" + b);
}
show(20, 30);
●定时器this
var jsonObj = {
"username": "tom",
"password": "123456"
}
setTimeout(function(jsonObj) {
alert("定时器" + this)
//定时器[object Window]
}, 1000);
//箭头函数 里面没有this
//此时的this是外层的this
setTimeout((jsonObj) => alert("定时器" + this), 3000);
●解构赋值
var [a, b, c] = [10, 20, 30];
alert(a + "=" + b + "=" + c);
// 10=20=30
●箭头函数体中的 this 对象,是定义函数时的对象,而不是使用函数时的对象。
function fn() {
window.setTimeout(() => {
// 定义时,this 绑定的是 fn 中的 this 对象
console.log(this.a);
}, 0)
}
//var a = 20;
// fn 的 this 对象为 {a: 18}
fn.call({
a: 18
}); // 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
Vue第一个案例
< html>
< head>
< meta charset = " utf-8" >
< title> title>
< script src = " js/vue.js" > script>
head>
< body>
< div id = " box" >
< h1> {{msg}} h1>
div>
body>
< script>
var app = new Vue ( {
el : '#box' ,
data : {
msg : 'hello Vue'
}
} ) ;
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
模板语法之插值表达式
< body>
< div id = " box" >
< h1> {{msg}} h1>
< h1> {{num+20*2}} h1>
< h1> {{num+20*2>200}} h1>
< h1> {{flag==false}} h1>
< h1> {{num>100?'大于':'小于'}} h1>
< h1> {{msg.split("").reverse().join("")}} h1>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
msg : 'hello' ,
num : 100 ,
flag : true
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
文本插入v-text 和v-html指令
< body>
< div id = " box" >
< h1> {{msg}} h1>
< h1 v-text = " msg" > h1>
< h2 v-html = " text" > h2>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
msg : 'hello' ,
text : '呵呵 '
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
不解析模板语法v-pre指令
< body>
< div id = " box" >
< h1 v-pre > {{msg}} h1>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
msg : 'hello'
}
} )
script>
< head>
< meta charset = " utf-8" />
< title> title>
< style type = " text/css" >
[v-cloak] {
display : none;
}
style>
< script src = " js/vue.js" > script>
head>
< body>
< div id = " box" >
< h1 v-cloak > {{msg}} h1>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
msg : 'hello'
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
属性显示隐藏
v-show指令
< body>
< div id = " box" >
< div v-show = " flag" style = " width : 200px; height : 200px; background : green; " > div>
< button @click = " change()" > 切换 button>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
flag : true
} ,
methods : {
change ( ) {
this . flag = ! this . flag;
}
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
条件语句v-if指令
< body>
< div id = " box" >
< div v-if = " flag" style = " width : 200px; height : 200px; background : green; " > div>
< button @click = " change()" > 切换 button>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
flag : true
} ,
methods : {
change ( ) {
this . flag = ! this . flag;
}
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
条件语句v-if和v-else指令
< body>
< div id = " box" >
< h1 v-if = " flag" > 我爱你 h1>
< h2 v-else > 我不爱你 h2>
< button @click = " change()" > 切换 button>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
msg : 'hello' ,
flag : true
} ,
methods : {
change ( ) {
this . flag = ! this . flag;
}
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
条件语句v-else-if指令
< body>
< div id = " box" >
< input type = " text" placeholder = " 请输入你的成绩 0---100" v-model = " score" >
< h1 v-if = " score>=0&&score<60" > 成绩不及格 h1>
< h1 v-else-if = " score>=60&&score<70" > 成绩很差 h1>
< h1 v-else-if = " score>=70&&score<80" > 成绩中等 h1>
< h1 v-else-if = " score>=80&&score<90" > 成绩良好 h1>
< h1 v-else-if = " score>=90&&score<100" > 成绩优秀 h1>
< h1 v-else-if = " score=100" > 满分 h1>
< h1 v-else > 成绩不合法 h1>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
score : 0
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
循环语句v-for指令
< body>
< div id = " box" >
< ul>
< li v-for = " (ele,index) in arr" v-if = " index%2==0" :key = " index" >
{{index}}----{{ele}}
li>
ul>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
msg : 'hello' ,
arr : [ 10 , 20 , 30 , 40 , 50 , 60 , 70 ]
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
循环遍历JSON对象
< body>
< div id = " box" >
< ul>
< li v-for = " (value,key,index) in jsonObj" :key = " index" >
{{index}}=={{key}}=={{value}}
li>
ul>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
jsonObj : {
"username" : "jack" ,
"password" : "123456" ,
"phone" : "13510733521" ,
"sex" : "男"
}
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
循环遍历JSON数组
< body>
< div id = " box" >
< ul>
< li v-for = " (obj,index) in jsonArray" :key = " index" >
{{obj.username}}--{{obj.password}}--{{obj.sex}}--{{obj.phone}}
li>
ul>
div>
body>
< script>
new Vue ( {
el : '#box' ,
data : {
jsonArray : [
{
"username" : "jack" ,
"password" : "123456" ,
"phone" : "13510733521" ,
"sex" : "男"
} ,
{
"username" : "tom" ,
"password" : "123456" ,
"phone" : "13510733521" ,
"sex" : "女"
} ,
{
"username" : "smith" ,
"password" : "123456" ,
"phone" : "13510733521" ,
"sex" : "男"
}
]
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
JSON对象增加删除属性的触发
< body>
< div id = " box" >
< ul>
< li v-for = " (value,key,index) in obj" :key = " index" >
{{key}}-------{{value}}
li>
ul>
< button type = " button" @click = " update()" >
给json对象中增加属性或删除属性要触发视图更新
button>
div>
body>
< script type = " text/javascript" >
new Vue ( {
el : '#box' ,
data : {
obj : {
"name" : "老沈" ,
"age" : 1 ,
"sex" : "男"
}
} ,
methods : {
update ( ) {
this . $set ( this . obj, 'sal' , '8888888' ) ;
this . $delete ( this . obj, 'age' ) ;
}
}
} )
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
发送Ajax请求
< head>
< script src = " js/vue.js" > script>
< script src = " js/vue-resource.min.js" > script>
head>
< body>
< div id = " box" >
< h1> {{city}} h1>
< button @click = " sendAjaxRequest()" > 发送Ajax之GET请求 button>
< button @click = " sendAjaxRequest2()" > 发送Ajax之POST请求 button>
< button @click = " sendAjaxRequest3()" > 发送Ajax之JSONP请求 button>
div>
body>
< script>
const app = new Vue ( {
el : '#box' ,
data : {
msg : 'HelloWorld' ,
city : '' ,
} ,
methods : {
sendAjaxRequest : function ( ) {
this . $http. get ( 'http://wthrcdn.etouch.cn/weather_mini?city=商洛' )
. then ( function ( res ) {
var jsonObj = res. body;
this . city = jsonObj. data. city;
} , function ( ) {
console. log ( '请求失败处理' ) ;
} ) ;
} ,
sendAjaxRequest2 ( ) {
this . $http. post ( '/try/ajax/demo_test_post.php' , {
name : "菜鸟教程" ,
url : "http://www.runoob.com"
} , {
emulateJSON : true
} ) . then ( function ( res ) {
document. write ( res. body) ;
} , function ( res ) {
console. log ( res. status) ;
} ) ;
} ,
sendAjaxRequest3 ( ) {
let url = 'https://www.baifubao.com/callback' ;
this . $http. jsonp ( url, {
params : {
phone : '15850781443' ,
cmd : '1059'
} ,
jsonp : 'callback'
} ) . then ( res => {
console. log ( res. data) ;
alert ( res. data. data. area) ;
} ) ;
}
}
} ) ;
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
Axios发送Ajax请求
< head>
< script src = " js/vue.js" > script>
< script src = " js/axios.min.js" > script>
< script src = " js/qs.min.js" > script>
head>
< body>
< div id = " box" >
< h1> {{city}} h1>
< h1> {{info}} h1>
< button @click = " sendAjaxRequest()" > 发送Ajax之GET请求 button>
< button @click = " sendPost()" > 发送Ajax之POST请求 button>
div>
body>
< script>
var qs = Qs
const app = new Vue ( {
el : '#box' ,
data : {
msg : 'HelloWorld' ,
city : '' ,
info : ''
} ,
methods : {
sendAjaxRequest ( ) {
axios. get ( 'http://wthrcdn.etouch.cn/weather_mini' , {
params : {
city : '商洛'
}
} )
. then ( response => {
console. log ( response) ;
var jsonObj = response. data;
this . city = jsonObj. data. city;
} )
. catch ( function ( error ) {
console. log ( error) ;
} ) ;
} ,
sendPost ( ) {
var s = qs. stringify ( {
"username" : "zhangsan" ,
"password" : "123456"
} )
axios. post ( 'http://localhost:8080/login' , s, {
headers : {
'Content-Type' : 'application/x-www-form-urlencoded'
}
} )
. then ( function ( response ) {
alert ( response. data. username) ;
alert ( response. data. msg) ;
} )
. catch ( function ( error ) {
console. log ( error) ;
} ) ;
}
}
} ) ;
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
Vue的所有生命周期函数
DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" />
< title> title>
< script src = " js/vue.js" > script>
head>
< body>
< div id = " box" >
{{data}}
< button type = " button" @click = " xiaohui()" > 销毁 button>
div>
< script type = " text/javascript" >
var vm = new Vue ( {
el : "#box" ,
data : {
data : "这是一个数据" ,
info : "这是另一个数据数据"
} ,
methods : {
xiaohui ( ) {
this . $destroy ( ) ;
}
} ,
beforeCreate : function ( ) {
console. log ( "beforeCreate()创建前========" )
console. log ( "数据:" + this . data)
console. log ( "模板:" + this . $el)
} ,
created : function ( ) {
console. log ( "created()已创建========" )
console. log ( "数据:" + this . data)
console. log ( "模板:" + this . $el)
} ,
beforeMount : function ( ) {
console. log ( "beforeMount()mount之前========" )
console. log ( "数据:" + this . data)
console. log ( "模板:" + this . $el)
} ,
mounted : function ( ) {
console. log ( "mounted()mounted========" )
console. log ( "数据:" + this . data)
console. log ( "模板:" + this . $el)
} ,
beforeUpdate : function ( ) {
console. log ( "beforeUpdate()更新前========" ) ;
} ,
updated : function ( ) {
console. log ( "updated()更新完成========" ) ;
} ,
beforeDestroy : function ( ) {
console. log ( "beforeDestroy()销毁前========" )
console. log ( "数据:" + this . data)
console. log ( "模板:" + this . $el)
} ,
destroyed : function ( ) {
console. log ( "destroyed()已销毁========" )
console. log ( "数据:" + this . data)
console. log ( "模板:" + this . $el)
}
} )
script>
body>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
过滤器
< body>
< div id = " box" >
< h1> {{msg|myfilterQJ|myfilterJB}} h1>
< h1> {{str|myfilterJB}} h1>
div>
body>
< script>
Vue. filter ( 'myfilterQJ' , function ( value ) {
return value. charAt ( 0 ) . toUpperCase ( )
. concat ( value. substring ( 1 ) . toLowerCase ( ) )
} )
new Vue ( {
el : '#box' ,
data : {
msg : 'hello' ,
str : '123456'
} ,
filters : {
'myfilterJB' : function ( value ) {
return value. split ( '' ) . reverse ( ) . join ( '' ) ;
}
}
} )
script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27