2016-11-06 3 views
2

に発生しません発する:

remove関数が呼び出される
export default { 
    props: ['note'], 
    methods: { 
     remove(){ 
      NoteRepo.remove(this.note, (err) => { 
       if (err) { 
        console.log('Should Fire') 
        this.$emit('alerted', { 
         type: 'error', 
         message: 'Failed to remove note' 
        }); 
       } 
      }) 
     } 
    } 
} 

、コンソールログは「火べき」が、$発するイベントが発生しません。コールバックの外に$ emitを移動すると、次のようになります。

export default { 
    props: ['note'], 
    methods: { 
     remove(){ 
      this.$emit('alerted', { 
       type: 'error', 
       message: 'Failed to remove note' 
      }); 

      NoteRepo.remove(this.note, (err) => { 
       if (err) { 
        console.log('Should Fire') 
       } 
      }) 
     } 
    } 
} 

これは機能します。私は_this = thisを割り当てて、それを使って$ emitを発火させましたが、違いはありません。

$ emitイベントがコールバックで起動しないのはなぜですか?

+0

矢印機能を使用しないようにしましたか?私はちょうどvueを使い始めましたが、私は、矢印関数がいくつかの場所で使用されたときに正しくコンテキストをバインドしないというドキュメントを読むことを覚えているようです。 – theWanderer4865

+0

ええ、私が正しく理解している場合、矢印関数を使用するとき、 'this'はVueインスタンスを表します。この場合は、矢印関数が問題ありません。つまり、私は試してみました(そしてまた、プロセスで '_self = this 'を割り当てます)、違いはありません。私はそれをエイリアスするためのあらゆる種類の方法を試しましたが、運はありません。 – evu

+1

ここで適切なビットが見つかりました:http://vuejs.org/v2/guide/instance.html#Properties-and-Methodsのように見える矢印関数は、VMインスタンスにバインドされていません。 – theWanderer4865

答えて

0

したがって、問題を引き起こしたのはNoteRepoのものだったことが判明しました。特にfirebaseイベントコールバック。

constructor() { 
     super(); 

     // Initialize Firebase 
     Firebase.initializeApp({ 
      apiKey: "xxx", 
      authDomain: "xxx", 
      databaseURL: "xxx", 
      storageBucket: "xxx", 
      messagingSenderId: "xxx" 
     }); 

     this.ref = Firebase.database().ref('notes'); 
     this.attachFirebaseListeners(); 
    } 

    attachFirebaseListeners() { 
     this.ref.on('child_added', this.onAdded, this); 
     this.ref.on('child_removed', this.onRemoved); // Removing 'this' here seems to of fixed it. 
     this.ref.on('child_changed', this.onChanged, this); 
    } 

私は正確に何が間違っていたのかよくわかりませんが、これは今のところ整理されているようです。

関連する問題