代码拉取完成,页面将自动刷新
npm install @vue/cli@3.0.3 -g
vue -V -->查看版本
vue create my-demo
yarn install
yarn run serve
yarn run build
yarn run test:unit
var name = 'id is ' + bid+ ' ' + aid + '.'
var url = 'http://localhost:5000/api/values/' + id
var name = `id is ${aid} ${bid}`
var url = `http://localhost:5000/api/values/${id}`//注意是反引号,英文输入下下的,Tab键上边的那个
//之前我们都是这么写的
var roadPoem = '这个是一个段落'
+ '换了一行'
+ '增加了些内容'
+ 'dddddddddd'
//但是在ES6中,可以使用反引号
var roadPoem = `这个是一个段落
换了一行
增加了些内容,
dddddddddd
结尾,`
function buyBook(price, count = 0.9){
return price * count;
}
buyBook(100);
//甚至可以将方法的值赋给参数
function buyBook(price, count =GetCount()){
return price * count;
}
function GetCount(){
return 100;
}
buyBook(200);
//ES6之前是这样的
function add(a,b,c){
let total = a + b + c;
return total;
}
add(1, 2, 3);
//ES6你可以这么操作,提供了 rest 参数来访问多余变量
function sum(...num) {
let total = 0;
for (let i = 0; i < num.length; i++) {
total = total + num[i];
}
return total;
}
sum(1, 2, 3, 4, 6);
var obj = {
data: {
books: "",
price: 0,
bookObj: null
},
bind() {//注意!ES6 中,可以使用这种方法简写函数,等价于 bind: function () {
var that = this;
//普通函数
//$(".ok").click(function () {
// console.log(this);//这个时候,this,就是 .ok 这个Html标签
// var bookItem = that.data.bookObj;
// var _parice = $(bookItem).data("price");
// var _book = $(bookItem).data("book");
// that.data.books += _book + ",";
// that.data.price += parseInt(_parice);
// that.show();
//});
//箭头函数
$(".ok").click(() => {
var bookItem = this.data.bookObj;//在箭头函数中,this指向的是定义函数时所在的对象
var _parice = $(bookItem).data("price");
var _book = $(bookItem).data("book");
this.data.books += _book + ",";
this.data.price += parseInt(_parice);
this.show();
$(".bg,.popupbox").hide();
});
},
}
在普通的click函数中 this 指向对象 $(".ok") ,因此,我们如果想要获取定义的对象中的数据(obj.data),那我们只能在 click 方法前,就去用一个 that 自定义变量来保存这个 this ,
但是在箭头函数中就不一样了,this 始终指向定义函数时所在的对象(就是 obj 对象);
是不是更方便些!
var vm = new Vue({
el:'#root',
data:{
tasks:[]
},
mounted(){
axios.get('/tasks')
.then(function (response) {
vm.tasks = response.data;//使用Vue实例
})
},
mounted2(){
axios.get('/tasks')
.then(response => this.tasks = response.data);//箭头函数 this
}
});
function blog(bl) {
if (bl) {
var foo = "Blog";
}
console.log(foo);
}
blog(true); //=> Blog
因此:var 定义的变量是函数级作用域,作用范围是在函数开始阶段和函数执行完成之前内都是存在的;
并且如果该函数内部还存在匿名函数等特殊函数,这个 var 出的变量在匿名函数中仍然可以用;
function Blog(bool) {
if (bool) {
let foo = "Blog";
} else {
console.log(foo);
}
}
Blog(false); //这里会报错 Uncaught ReferenceError: foo is not defined
因此,使用 let,上述问题完全解决,let出的变量作用域是 块作用域,在离开某一代码块,该变量就会被销毁不存在
应当尽可能的避免用 var,用 let 来代替,除非你需要用到变量提升。
const 与 let 的基本用法相同,定义的变量都具有块级作用域,也不会发生变量提升。不同的地方在于,const 定义的变量,只能赋值一次。
const foo='Blog';
function Blog(bool) {
if (bool) {
foo = "Vue";
} else {
console.log(foo);
}
}
Blog(true); //这里会报错 Identifier 'foo' has already been declared
因此const多用作不发生变化的变量定义,比如定义月份,或者,星期等:const months = [];
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。