1

我是聚合物的新手,我有一些问题。我的聚合物火灾注册页面上有以下代码聚合物代码。

 Polymer({ 
     is: 'my-register', 
     properties: { 
      message: { 
       type: String, 
       value: '', 
      }, 
      email:{ 
       type: String, 
       value: '', 
      }, 
      password: { 
       type: String, 
       value: '', 
      }, 
      user: { 
       type: Object, 
       notify: true, 
      }, 
      customUser: { 
       value: {}, 
       notify: true, 
      }, 
     }, 
     loginSuccess: function(){ 
      this.customUser['status'] = 1; 
      if(this.customUser['status'] == 1 && this.message == ""){ 
       console.log("done"); 
       // this.$.ironLocation.set('path', '/profile'); 
      } 
     }, 
     createUserWithEmailAndPassword: function() { 
      this.error = null; 
      this.$.auth.createUserWithEmailAndPassword(this.email, this.password) 
       .then(function(response) { 
        this.loginSuccess(); 
        console.log("success"); 
       }) 
       .catch(function(error) { 
        console.log("Error"); 
       }); 
      this.password = null; 
     }, 
     ready: function(){ 
      this.customUser['status'] = 0; 
     }, 
     handleError: function(e) { 
      this.message = 'Error: ' + e.detail.message; 
     }, 
     signOut: function() { 
      this.error = null; 
      this.$.auth.signOut(); 
     }, 
    }); 

问题是我无法从createUserWithEmailAndPassword成功函数调用loginSuccess函数。可以从firebase-auth创建函数访问外部方法吗?

有没有办法跟踪firebase用户对象中的自定义属性,而不是创建第二个自定义用户对象?我不认为这是非常有效的,因为我必须在整个应用程序中访问这两个属性。

+0

除了为自定义用户信息创建自定义用户之外没有其他可能性。不知道,但尝试绑定(这)到创建函数 –

+0

你究竟是什么意思绑定(这)?将此作为参数传递给创建函数? –

+0

尝试:.then(函数(响应){this.loginSuccess(); console.log(“success”); } .bind(this))。或者为了更清洁的解决方案,您还可以尝试使用箭头功能(es6):。然后((响应)=> this.loginSuccess(); console.log(“success”); })。 Neiro的答案可能会工作,但不是很干净 –

回答

0

你可以试试这个..

createUserWithEmailAndPassword: function() { 
    var self = this; 
    // Or ES6 
    // let self = this;  
    this.error = null; 
    this.$.auth.createUserWithEmailAndPassword(this.email, this.password) 
    .then(function (response) { 
     self.loginSuccess(); 
     console.log("success"); 
    }).catch(function (error) { 
     console.log("Error"); 
    }); 

    this.password = null; 
} 

我用这一招,并为我工作。

干杯..!

+1

在es6中,当使用箭头函数时,您不需要使用self = this操作 –