Polymer 如何在linkWithPopup函数之后调用函数

Polymer 如何在linkWithPopup函数之后调用函数,polymer,web-component,Polymer,Web Component,当linkWithPopup解析为以下代码时,我试图调用函数: _linkToCurrentUser: function(authId, provider) { this.user.linkWithPopup(provider).then(function(result) { console.log("user linked ", result); this.updateUserInfos(authId); // <-- error: this.updateUserIn

linkWithPopup
解析为以下代码时,我试图调用函数:

_linkToCurrentUser: function(authId, provider) {
  this.user.linkWithPopup(provider).then(function(result) {
    console.log("user linked ", result);
    this.updateUserInfos(authId); // <-- error: this.updateUserInfos is not a function
  }).catch(function(error) {
    console.log(error);
  });
},
updateUserInfos: function(authId) {
  ...
}
但是,该功能在
链接WithPopup
功能之外时工作:

_linkToCurrentUser: function(authId, provider) {
  this.user.linkWithPopup(provider).then(function(result) {
    console.log("user linked ", result);
  }).catch(function(error) {
    console.log( error);
  });

  this.updateUserInfos(authId); // <-- works
},
updateUserInfos: function(authId) {
  ...
}
\u链接到当前用户:函数(authId,提供程序){
this.user.linkWithPopup(提供者)。然后(函数(结果){
日志(“用户链接”,结果);
}).catch(函数(错误){
console.log(错误);
});

this.updateUserInfo(authId);//此问题是回调的上下文未绑定到Polymer对象,因此它使用外部上下文(通常是
窗口
对象)来

您可以切换到(假设ES6对您可用)以自动绑定聚合物对象:

_linkToCurentUser: function(authId, provider) {
  this.user.linkWithPopup(provider).then(result => { // <-- bind `this` to Polymer object
    console.log('user linked', result);
    this.updateUserInfos(authId);
  })
  ...
}
_linkToCurentUser: function(authId, provider) {
  this.user.linkWithPopup(provider).then(function(result) {
    console.log('user linked', result);
    this.updateUserInfos(authId);
  }.bind(this))  // <-- bind `this` to Polymer object
  ...
}
或者您可以传入一个引用:

_linkToCurentUser: function(authId, provider) {
  var self = this; // <-- cache reference for use in callback
  this.user.linkWithPopup(provider).then(function(result) {
    console.log('user linked', result);
    self.updateUserInfos(authId);
  })
  ...
}
\u linkToCurentUser:function(authId,provider){
var self=这个//
_linkToCurentUser: function(authId, provider) {
  var self = this; // <-- cache reference for use in callback
  this.user.linkWithPopup(provider).then(function(result) {
    console.log('user linked', result);
    self.updateUserInfos(authId);
  })
  ...
}