[英]Function doesn't wait for async/await on addListener?
据说,该组件需要在更改路由之前等待notificationsMixin
Mixin 完成,但它不需要:
混入:
export const notificationsMixin = {
methods: {
async notificationsMixin () {
this.$Plugins.PushNotifications.register()
this.$Plugins.PushNotifications.addListener('registration', async (token) => {
await this.API(token.value)
})
this.$Plugins.PushNotifications.addListener('registrationError', () => {
//
})
},
async API (token) {
await this.$axios.post('https://api.fexler.com/?action=notifications', token).then(async (response) => {
if (response.data) {
await this.$Plugins.Storage.set({
key: 'token',
value: JSON.stringify(token)
})
}
})
}
}
}
成分:
this.notificationsMixin().then(async () => {
this.$router.push('/profile')
})
此功能立即解决。
async notificationsMixin () {
this.$Plugins.PushNotifications.register()
this.$Plugins.PushNotifications.addListener('registration', async (token) => {
await this.API(token.value)
})
this.$Plugins.PushNotifications.addListener('registrationError', () => {
//
})
},
尝试添加await
async notificationsMixin () {
try {
await this.$Plugins.PushNotifications.register()
const token = await this.$Plugins.PushNotifications.addListener('registration')
await this.API(token.value)
this.$Plugins.PushNotifications.addListener('registrationError', () => {
//
})
}
catch (e) {
// handle error
}
}
我不是 100% 熟悉你使用的这个插件,所以你可能需要稍微调整一下代码。 但这应该让您知道该怎么做。
作为额外的提示,将then
与await
混合并不是很好的做法(IMO)。 选择一个并坚持下去。
并且您在这里不需要async
:
this.notificationsMixin().then(async () => {
this.$router.push('/profile')
})
改成:
this.notificationsMixin().then(() => {
this.$router.push('/profile')
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.