ES6语法
●变量拼接
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
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>
模板语法之插值表达式
<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>
文本插入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:'<s>呵呵</s>'
}
})
</script>
不解析模板语法v-pre指令
<body>
<div id="box">
<h1 v-pre>{
{msg}}</h1>
</div>
</body>
<script>
new Vue({
el:'#box',
data:{
msg:'hello'
}
})
</script>
解决初始化v-cloak指令
<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>
属性显示隐藏
v-show指令
<body>
<div id