在vuejs中使用axios进行全局错误处理

时间:2018-09-11 19:19:04

标签: vue.js axios

我在vuejs中使用axios,在axios调用时遇到了全局错误处理的麻烦。我正在尝试以下方法,它不仅需要用户注销,而且还要在axios调用后执行代码块。   如果发生错误,如何阻止axios呼叫承诺代码部分的执行。

export default {
    created() {
    var self = this;
      if (self.$route.meta.requiresAuth) {
        axios.interceptors.response.use(
          function(response) {
            return response;
          },
          function(error) {
            if (error.response.status == 401) {
              self.$toaster.error("ERROR: Invalid token detected");
                self.logout();
              }
            }
          );
        }
      },
      methods: {
        logout() {
          this.$router.push({ name: "login" });
        }
      }
    }

someFunction() {
  var self = this;
  axios
    .post(
      "url/to/api",
      {},
      {
        headers: {
          Authorization: "authtoken here"
        }
      }
    )
    .then(response => {
      alert("success");
        otherFunction();
      })
      .catch(error => {
        console.log(error);
      });
}

1 个答案:

答案 0 :(得分:0)

您将需要从错误拦截器中引发一个新错误。

#libraries
 library(DBI)
 library(rJava)
 library(RJDBC)

#the driver is a JDBC if that helps
con <- dbConnect(drv, url "some url")
my_query = "select * from my_table"
print(my_query)
server <- function(input, output, session){
}
ui=shinyUI(fluidPage(
))
shinyApp(ui = ui, server = server)
相关问题