axios设置默认路径问题?

axios.defaults.baseUrl = process.env.API_ROOT || "//localhost:3000";这样设置的默认路径,

import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App'
import router from './router'
import axios from 'axios'
import $ from 'jquery';
Vue.use(ElementUI);

library.add(faCoffee)

Vue.component('font-awesome-icon', FontAwesomeIcon)

Vue.config.productionTip = false

axios.defaults.baseUrl = process.env.API_ROOT || "//localhost:3000";
Vue.prototype.$axios = axios;
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>'
})

在页面中其它页面引用,报错post 未定义,这个应该怎么改?

    this.$axios.post('/api/login', {
      username: this.username,
      password: this.password
    }).then( res => {
阅读 4.1k
1 个回答

不是你的路径问题,是不是你在其他js文件里使用this.$axios,这个是不允许的。需要引入axios文件,再进行调用axios

比如你在index.js里需要使用axios

// index.js
import axios from 'axios'

axios.post('/api/login', {
  username: this.username,
  password: this.password
}).then( res => {})

this.$axios只能在.vue文件里访问。如果在其他JS包里使用,则需要重新引入axios

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进