下面我有以下内容:
private setupSocketEventListeners() {
this.socketServiceProvider.on('startTheGame', questions => {
this.navCtrl.push(PlayGamePage, {
questions: questions,
isTheHost: this.isTheHost(),
gameId: this.getGameId()
}).then(() => {
let index = 1
this.navCtrl.remove(index)
})
})
this.socketServiceProvider.on('updatePlayersInGame', players => {
players.forEach(player => {
this.players.push(player)
})
})
this.socketServiceProvider.on('playerLeft', playerName => {
this.players.pop()
})
this.socketServiceProvider.on('playerJoined', player => {
this.players.push({ name: player })
})
this.socketServiceProvider.on('hostLeft', () => {
if (!this.isTheHost()) {
this.alertCtrl.create({
message: 'The host left the game',
buttons: [{
text: 'Ok'
}]
}).present()
}
this.socketServiceProvider.emit('leaveGame', {
gameId: this.getGameId(),
playerName: this.getPlayerName()
})
})
}
现在在ionViewDidLoad生命周期钩子中调用它。我面临的问题是,当我到达此页面时,我可能会点击后退按钮。因此,为了解决这个问题,我将执行以下操作(这也在inViewDidLoad中调用)
this.navbar.backButtonClick = () => {
this.navCtrl.pop()
this.socketServiceProvider.emit('leaveGame', {
gameId: this.getGameId(),
playerName: this.getPlayerName()
})
}
所以这将删除添加到堆栈的HostGamePage,或者我认为会这样。当我回到HostGamePage并点击开始游戏按钮时,它会调用'this.navCtrl.push(PlayGamePage'事件两次,或者我回去后多次回到页面。现在,我的套接字事件没有发出两次,但实际的页面正在执行两次代码。我无法弄清楚为什么和我做错了但我现在完全被卡住了
我确实通过this.app.getActiveNav().setRoot(PlayGamePage, {})
解决了这个问题,但这打破了我的应用程序中的所有导航。