Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在axios get请求中返回值?_Javascript_Vue.js_Axios_Es6 Promise_Vue Router - Fatal编程技术网

Javascript 如何在axios get请求中返回值?

Javascript 如何在axios get请求中返回值?,javascript,vue.js,axios,es6-promise,vue-router,Javascript,Vue.js,Axios,Es6 Promise,Vue Router,我正在做一个Vue项目。现在我正在通过JWT处理身份验证。为此,我正在创建一个模块“axios.js”,该模块应导入main.js中,以允许令牌验证,并因此向用户授予权限 其主要思想是将令牌传递给端点/endpoint,并让它返回状态代码 我目前的问题是,我不能返回状态码,我得到的是一个承诺,而不是布尔值 auth.js import axios from "axios"; var config = { headers: { Authorization: "Bearer " + local

我正在做一个Vue项目。现在我正在通过JWT处理身份验证。为此,我正在创建一个模块“axios.js”,该模块应导入main.js中,以允许令牌验证,并因此向用户授予权限

其主要思想是将令牌传递给端点
/endpoint
,并让它返回状态代码

我目前的问题是,我不能返回状态码,我得到的是一个承诺,而不是布尔值

auth.js

import axios from "axios";

var config = {
  headers: { Authorization: "Bearer " + localStorage.getItem("usertoken") }
};

export default async function isAuth() {
  let responseStatus = axios.get("http://localhost:5000/endpoint", config);

  let status = await responseStatus.then(res => {
    if (res.status == 200) {
      return true;
    } else {
      return false;
    }
  });
  return status;
}
import isAuth from "./axios/auth.js";

const router = new VueRouter({
  mode: "history",
  routes
});

router.beforeEach((to, from, next) => {
  // eslint-disable-next-line no-console
  console.log(isAuth());
  var requiresAuth = to.matched.some(record => record.meta.requiresAuth);

  if (requiresAuth && !isAuth()) {
    next("/login");
  } else if (to.path == "/login" && isAuth()) {
    next("/home");
  } else {
    next();
  }
});
main.js文件的相关部分如下所示。其余的是进口和路线,这里可能不需要

main.js

import axios from "axios";

var config = {
  headers: { Authorization: "Bearer " + localStorage.getItem("usertoken") }
};

export default async function isAuth() {
  let responseStatus = axios.get("http://localhost:5000/endpoint", config);

  let status = await responseStatus.then(res => {
    if (res.status == 200) {
      return true;
    } else {
      return false;
    }
  });
  return status;
}
import isAuth from "./axios/auth.js";

const router = new VueRouter({
  mode: "history",
  routes
});

router.beforeEach((to, from, next) => {
  // eslint-disable-next-line no-console
  console.log(isAuth());
  var requiresAuth = to.matched.some(record => record.meta.requiresAuth);

  if (requiresAuth && !isAuth()) {
    next("/login");
  } else if (to.path == "/login" && isAuth()) {
    next("/home");
  } else {
    next();
  }
});
使用下面main.js上的
console.log(isAuth())
找到打印屏幕:


如果需要更多信息,请告诉我!谢谢。

Axios.get已经返回了一个承诺,所以您可以从
isAuth
函数返回
Axios.get(…)
。然后,在使用
isAuth
函数的地方,可以运行
isAuth()。然后(…)
或通过执行
wait isAuth(),使用
async
/
wait

从“./axios/auth.js”导入isAuth
变量配置={
标题:{
“授权”:“承载者”+localStorage.getItem('usertoken')
}
};
导出默认异步函数isAuth(){
//Axios.get已返回承诺,因此只需返回Axios.get即可
返回axios.get(“http://localhost:5000/endpoint“,配置);
}
常量路由器=新的VueRouter({
模式:“历史”,
路线
})
路由器.beforeach((到、从、下一个)=>{
//eslint禁用下一行无控制台
console.log(isAuth())//这将只记录一个承诺。
var requiresAuth=to.matched.some(record=>record.meta.requiresAuth);
isAuth()。然后(结果=>{
如果(结果){
//结果成功
}否则{
//结果失败
}
});
});
//或者,使用异步/等待
路由器.beforeach(异步(到、从、下一个)=>{
//eslint禁用下一行无控制台
console.log(isAuth())//这将只记录一个承诺。
var requiresAuth=to.matched.some(record=>record.meta.requiresAuth);
const isAuth=等待isAuth();
如果(已授权){
//结果成功
}否则{
//结果失败
}

});你好@TylerRoper,谢谢你的快速回答!我正在尝试使用
.then()
中建议的方法,但似乎无法从
isAuth()
函数返回
true
false