问题是
this
里面
events()
实际上不是您的Vue实例。您可以通过声明
events
作为一个
arrow-function
:
this.eventSources = [{
events: (start,end,timezone,callback) => {
alert(this.myId);
}
}]
new Vue({
el: '#app',
data() {
return {
eventSources: [],
myId: 1
};
},
methods: {
myMethod() {
this.eventSources = [{
events: (start,end,timezone,callback) => {
alert(this.myId);
}
}]
this.eventSources[0].events(0, 1, 'UTC', data => console.log(data))
}
}
})
<script src="https://unpkg.com/vue@2.5.16"></script>
<div id="app">
<button @click="myMethod">Click</button>
</div>