我正在尝试在全局路由器防护中使用导入的功能。但我收到此错误:
getCurrentStage is not defined
getCurrentStage
是从shops.js
导入的
shop.js
看起来像这样
import axios from 'axios'
export function getCurrentStage() {
axios.get("/admin/wine_app/shops").then(response => {
return response.data.current_stage;
});
}
export default getCurrentStage
然后我这样导入它:import getCurrentStage from '../api/shops'
然后尝试在我的全局路由器防护中使用它:
router.beforeEach((to, from, next) => {
if(to.name == null){
let current_stage = getCurrentStage();
}
else {
next();
}
})
但是我得到了未定义的错误。有帮助吗?
答案 0 :(得分:0)
您在导出的函数中未返回任何内容。您需要返回axios承诺,以便current_stage
返回一些信息:
export function getCurrentStage() {
return axios.get("/admin/wine_app/shops").then(response => {
return response.data.current_stage;
});
}
这只是要返回承诺,因此您需要使用该承诺做一些适当的异步操作,例如await
或.then
。例如:
getCurrentStage().then(stage => {
current_stage = stage;
});
您还将导出函数两次,这可能不是故意的。如果仅打算通过导入默认值来检索它,则不需要第一个export
。