繁体   English   中英

在 Vue 拦截器中使用 await

[英]Using await in Vue interceptor

我如何在拦截器中使用等待? 我试过下面的代码,但它不打印值。

http.interceptors.push(async (req, next) => {
    final value = await someAsyncFunction();
    console.log(value);
})

我也尝试了这段代码,它有效但我想使用 await 因为我需要在它之后同步执行一些代码。

http.interceptors.push(async (req, next) => {
    someAsyncFunction().then(value => console.log(value));
    executeSomethingElse();
})

我认为你不能在拦截器上使用匿名 function,所以你应该这样写

http.interceptors.push(async function (req, next) {
    await someAsyncFunction()
})

这是因为 Vue 无法访问后台的this属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM