javascript - 关于vue中this指向的一些问题?
PHPz
PHPz 2017-04-11 12:54:27
0
1
319

这个地方会报cannot read property push of undefined 。这个this好像指向的不是vue实例了。

在callMethod外面定义
var _this=this
因为在方法内部this指向的就变了。

下面再用:
_this.$router.push('home/baby')

PHPz
PHPz

学习是最好的投资!

répondre à tous(1)
小葫芦

JS中的this是运行时确定的。

function f() {
  console.log(this);
}

var obj = {};

f(); // Window;
f.apply(obj);  // obj

回到问题:
由于complete是jsonp的回调函数,所以,他的运行时是这样调用的window.complete。 可以看jsonp请求的返回。所以咯,complete中的this是Window,并不是Vue。

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!