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

Categories

0 votes
552 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 Reply

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'
    })
  }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...