2016-09-22 107 views
0

聲明我有Vue公司的運行形式,看起來像這樣的數據:vue.js設置如果功能

new Vue({ 
      el: '#login-container', 
      data: { 
       firstname: '', 
       lastname: '', 
       email: '', 
       birthday: '', 
       signupEmail: '', 
       password: '', 
       signupPassword: '', 
       confirm: '', 
       error: '', 
      }, 
      methods: { 

       login(){ 

        CH.INSTANCE.Services.Login(this.email, this.password, login_onComplete, login_onCancel); 

        function login_onComplete(aUser) 
        { 
         window.location.href = '/'; 
        } 

        function login_onCancel(aMessage) 
        { 
         this.error = aMessage ; 
        } 
       }, 

       signup(){ 

         CH.INSTANCE.Services.CreateAccount(this.firstname,this.lastname,this.signupEmail, this.signupPassword,'83835', 'male',this.birthday,':checked',onRegister_onComplete,onRegister_onError); 

         function onRegister_onComplete(aUser) 
         { 
          window.location.href = '/'; 
         } 

         function onRegister_onError(aMessage) 
         { 
          this.error = aMessage; 
         } 
       } 

      } 
     }) 

它工作正常減去的this.error = aMessage ;

aMessage將包含錯誤消息,應該在我的窗體上{{error}}中轉儲出錯信息。問題是它確實被設置。

如果我在方法的開始處設置this.error = 'test' ;以外的if,它的作用是當它被調用時。

如果我只是console.log(aMessage)if也適用。

不確定爲什麼它在設置時工作。

回答

0

問題是在這些函數中,this沒有引用你的Vue實例。我會設置你的功能是這樣的:

login() { 
    CH.INSTANCE.Services.Login(this.email, this.password, this.login_onComplete, this.login_onCancel); 
}, 
login_onComplete(aUser) { 
    window.location.href = '/'; 
}, 
login_onCancel(aMessage) { 
    this.error = aMessage ; 
} 
+0

Ahhhhh是有道理的。我仍然用Vue和JS包裹着所有的'this' – Packy