如何在vue-router中使用Axios

时间:2019-01-08 12:00:30

标签: vuejs2 axios

我想知道如何在beforeEach()的vueRouter中使用Axios

我确实是这样:

Command-B

结果为错误:

axios未定义

我不能使用'this',因为'this'是VueRouter

你知道怎么做吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您必须导入它:

import axios from 'axios'

const http = axios.create({
  baseURL: 'http://localhost:3000/',
})

...

http.get('me', {
    headers: {
        'x-access-token': $cookies.get('user_session') //the token is a variable which holds the token
    }
    })
    .then(response => {
        if (response.status == "200") {
            console.log('tet3');
        }
    }, response => {
        this.$cookies.remove("user_session");
        this.$router.push('/')
        //window.location.href = "/";
        console.log('erreur', response)
    })

...