2017-04-11 64 views
2

調用一個方法很建設vuejs組件

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(); 
     }) 
    }) 

我得到的錯誤是

this.filterPeople(); is not a function 

當我移動它的Event.$on塊它調用該方法之外。

這是如何工作的?

回答

1

使用箭頭功能保存上下文:

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

+0

尼斯還沒有真正做到ES6語法,但是這看起來整潔。 – LeBlaireau

3

這是所有與範圍做

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