代码之家  ›  专栏  ›  技术社区  ›  Ali Bedaer

我想在一个请求完成后执行axios请求

  •  0
  • Ali Bedaer  · 技术社区  · 8 年前

    我有一个axios请求,它获取一些数据并将其存储在本地存储中。我想在第一个请求完成后再发出一个请求,并使用第一个请求-响应数据

    this.$http.post("http://localhost:8000/oauth/token", data).then(response => { 
          this.$auth.setToken(
            response.data.access_token,
            response.data.expires_in + Date.now()
          );
        }).then(()=>{
          this.$http.get("user").then(response => {
            this.$auth.setAuthenticatedUser(response.data);
            this.user = response.data;
            this.image = response.data.image;
            this.$bus.$emit('logged_user',response.data);
          });
    
          this.$http
            .get("http://localhost:8000/api/tpa/provider/status")
            .then(res => {
              localStorage.setItem("tpa_provider", JSON.stringify(res.data));
          });
    
          this.$bus.$emit('logged_user',this.user);
    
          if(this.$auth.isAuth()){
            this.$router.push({"name":"home"});
          }
    

    我也尝试过使用async await,但无法实现

    1 回复  |  直到 8 年前
        1
  •  1
  •   dhilt    8 年前

    return .

    this.$http.post("http://localhost:8000/oauth/token", data).then(response => {
      // ...
      return response;
    }).then((myResponse) => {
      console.log('first result', myResponse);
      // ...
    });
    

    let myResponse;
    
    this.$http.post("http://localhost:8000/oauth/token", data).then(response => {
      myResponse = response;
      // ...
    }).then(() => {
      console.log('first result', myResponse);
      // ...
    });
    
    推荐文章