2017-04-11 46 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(); 
})