Vue中 let _this = this的作用
原文鏈接:《Vue.js的this作用域》https://blog.csdn.net/love_pgme/article/details/86064871
1、先舉一個例子,像 jquery里邊有一個特別典型的例子,能說明用_this的作用
$("#btn").click(function(){
var _this = this; //這里this和_this都代表了"#btn"這個對象
$(".tr").each(function(){
this; //在這里this代表的是每個遍歷到的".tr"對象
_this;//仍代表"#btn"對象
})
})
這種情況就是在一個代碼片段里this有可能代表不同的對象,而編碼者希望_this代表最初的對象。
2、Vue的寫法:let _this = this,其目的是為了存儲this的指向。可以通過觀察下面的例子來加深理解。

var _this = this; 這里聲明一個變量指向父函數的this, 用於 _this.DataNow = new Date(); 修改父函數的“DataNow”實現動態時間
如果這里改為this.DataNow = new Date() 就指向了mounted里面的DataNow,則沒有了效果;
