【发布时间】:2018-10-22 17:19:47
【问题描述】:
试图挂载一个 Vue.js 组件,但似乎第二个函数 (eventSelection) 被调用得比预期的要早。我应该怎么做才能使其仅在第一个功能完成后才执行?
...
mounted() {
this.getAllEvents()
this.eventSelection()
},
methods: {
getAllEvents: function () {
console.log("Cheguei aqui");
getEvents()
.then( function (res) {
console.log("Entrei then")
this.events = res.data
}.bind(this))
.catch( function (err){
debugger;
console.error('WeekSimulation, getEvents() ', err)
this.events = ["Seu chefe o convida para um happy hour com os diretores no final do expediente. Ao mesmo tempo, você recebe uma mensagem de seu cônjuge lembrando da apresentação no colégio do seu filho. O que você faz?", "Você tem muito trabalho a fazer, porém o tempo com sua família anda escasso. No final do expediente você escolheria jantar com sua família ou fazer hora extra?",
"Você acorda de manhã e seu filho não está se sentindo bem. Ao verificar sua agenda, lembra que tem uma reunião com um novo cliente em uma hora. Você leva seu filho ao médico ou vai para a reunião?",
"Ao checar o seu celular durante uma reunião com os diretores de sua organização, nota que recebeu cinco ligações de seu cônjuge. Você continua na reunião, ou pede para atender o telefone?"]
}.bind(this))
},
eventSelection: function() {
console.log("Funcao de selecao de evento")
debugger;
this.selectedEvent = _.shuffle(this.events)[0]
console.log(this.selectedEvent)
}
}
...
【问题讨论】:
-
努力加分,不过有点臃肿不行吗?
-
将 this.events 设置为 getEents 的结果,如下所示:
this.events=getEents().then(d=>d.data)和 eventSelection:this.events.then(events=>this.selectedEvent = _.shuffle(this.events)[0])
标签: javascript vuejs2 vue-component