2017-04-11 4 views
2

bin ein vuejs Komponente

methods: { 
     filterPeople: function() { 
     console.log('hello') 
     //do some good stuff 
     }, 


    }, 
    beforeCreate() { 
    //do some ajax stuff 

    }, 
    mounted() { 
     Event.$on('applied', function() { 
     console.log('the event is being caught') 
     this.filterPeople(); 
     }) 
    }) 

Der Fehler ich Gebäude ist

this.filterPeople(); is not a function 

Als ich es außerhalb des Event.$on Block bewegen sie die Methode tut nennen.

Wie funktioniert das?

Antwort

1

Mit dem Pfeil Funktionen zu tun, um den Kontext zu speichern:

Event.$on('applied',() => this.filterPeople());

+0

Nizza haben nicht wirklich ES6 Syntax getan, aber das sieht ordentlich. – LeBlaireau

3

Es war alles mit Scope

var self = this; 
     Event.$on('applied', function() { 
     self.filterPeople(); 
}) 
Verwandte Themen