Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

vuejs2 - 必须在vue / cli 4.0.5应用程序的任何* .vue文件中导入axios(Have to import axios in any *.vue file of vue/cli 4.0.5 app)

In my @vue/cli 4.0.5 app in any *.vue file I have to import axios, if I need to use it on this page, like:

(在我的@ vue / cli 4.0.5应用程序的任何* .vue文件中,如果我需要在此页面上使用它,则必须导入axios,例如:)

<script>
    import {bus} from '../../../src/main'
    import appMixin from '@/appMixin'
    import axios from 'axios'
    ...

But I expected axios to be accessible in *.vue file of my app.

(但是我希望可以在我的应用程序的* .vue文件中访问axios。)

In src/main.js I have defined :

(在src / main.js中,我定义了:)

import router from './router'
import store from './store'
import axios from 'axios'
...

Vue.use(axios)

moment.tz.setDefault(settingsTimeZone)
export const bus = new Vue()

axios.defaults.crossDomain = true
Vue.config.productionTip = false
Vue.prototype.$http = axios

I have this question as priorly I worked in laravel 5 / vuejs 2.6 app I have axios was accessible in all *.vue files of my app without any definition... Why so and if I have to write

(我有这个问题,因为我之前在laravel 5 / vuejs 2.6应用程序中工作过,我的axios可以在应用程序的所有* .vue文件中访问,而没有任何定义...为什么这样,如果我必须写)

import axios from 'axios'

in any file where I need axios ?

(在我需要axios的任何文件中?)

Thanks!

(谢谢!)

  ask by Petro Gromovo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

(尝试这个:)

Vue.use({
    install (Vue) {
    Vue.prototype.$api = axios.create({
      baseURL: 'PUT A BASE URL IF YOU WANT'
    })
  }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...