2017-09-21 73 views
0

如何將VueJs方法範圍綁定到返回的Firebase承諾?爲了調用VueJS莫代爾如何將VueJS方法範圍綁定到Firebase承諾

login: function() { 
      fireAuth.signInWithEmailAndPassword(this.signIn.email, this.signIn.password) 
       .then(function (user) { 
        if (!user.emailVerified) { 
         this.$modal.show('email_verification', { 
          text: 'Please verify your email' 
         }) 
        } 
       }, function (error) { 
        console.log(error.message); 
       }) 
     } 

現在,這裏是錯誤:

TypeError: Cannot read property '$modal' of undefined

回答

1

使用在你的承諾回調箭頭功能,使this在詞法勢必

login: function() { 
      fireAuth.signInWithEmailAndPassword(this.signIn.email, this.signIn.password) 
       .then((user) => { 
        if (!user.emailVerified) { 
         this.$modal.show('email_verification', { 
          text: 'Please verify your email' 
         }) 
        } 
       }, (error) => { 
        console.log(error.message); 
       }) 
     } 

或聲明var self指向方法內的vue實例,並使用self來使用vue實例,因爲回調函數現在將在之上關閉變量

login: function() { 
      var self = this; 
      fireAuth.signInWithEmailAndPassword(this.signIn.email, this.signIn.password) 
       .then(function (user) { 
        if (!user.emailVerified) { 
         self.$modal.show('email_verification', { 
          text: 'Please verify your email' 
         }) 
        } 
       }, function (error) { 
        console.log(error.message); 
       }) 
     }