Vuejs2 必须在vue/cli 4.0.5应用程序的任何*.vue文件中导入axios
在任何*.vue文件的@vue/cli 4.0.5应用程序中,如果需要在此页面上使用axios,我必须导入它,如:Vuejs2 必须在vue/cli 4.0.5应用程序的任何*.vue文件中导入axios,vuejs2,Vuejs2,在任何*.vue文件的@vue/cli 4.0.5应用程序中,如果需要在此页面上使用axios,我必须导入它,如: <script> import {bus} from '../../../src/main' import appMixin from '@/appMixin' import axios from 'axios' ... 我有这个问题,因为我以前在laravel 5/vuejs 2.6应用程序中工作,我的应用程序的所有*.vue文件都可
<script>
import {bus} from '../../../src/main'
import appMixin from '@/appMixin'
import axios from 'axios'
...
我有这个问题,因为我以前在laravel 5/vuejs 2.6应用程序中工作,我的应用程序的所有*.vue文件都可以访问axios
没有任何定义。。。
为什么会这样?如果我要写的话
import axios from 'axios'
在我需要axios的任何文件中
更新的块:
In my src/main.js I tried and failed as :
...
import axios from 'axios'
export const bus = new Vue()
axios.defaults.crossDomain = true
// axios.withCredentials = true
// window.axios.defaults.baseURL = window.App.baseurl;
Vue.config.productionTip = false
Vue.prototype.$http = axios
const token = localStorage.getItem('token')
if (token) {
Vue.prototype.$http.defaults.headers.common['Authorization'] = token
}
// home url in .env file
let apiUrl = process.env.VUE_APP_API_URL
alert( '++apiUrl::'+apiUrl ) // CHECK IT VALID URL
new Vue({
router,
store,
bus,
render: h => h(App)
}).$mount('#app')
Vue.use({
install (Vue) {
Vue.prototype.$api = axios.create({
baseURL: apiUrl // !!!
})
}
})
router.afterEach((to, from) => {
bus.$emit('page_changed', from, to)
})
但接下来在我的组件中,我用axios import注释了行:
<script>
import {bus} from '../../main'
// import axios from 'axios'
import Vue from 'vue'
...
export default {
data: function () {
return {
...
}
}, // data: function () {
mounted() {
this.loadTasksData()
}, // mounted() {
methods: {
loadTasksData() {
let apiUrl = process.env.VUE_APP_API_URL
let config = {
withCredentials: true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
}
}
axios({url: apiUrl + '/tasks_paged/' + this.current_page, data: {}, method: 'get', config: config})
.then(response => { // Error here!
怎么了?我把你的作品放在正确的地方了吗?
另外,你能给更多的解释(或提供一个链接)关于这个建设?这是关于我的经验。。。
谢谢 试试这个:
Vue.use({
install (Vue) {
Vue.prototype.$api = axios.create({
baseURL: 'PUT A BASE URL IF YOU WANT'
})
}
})
谢谢请查看更新的区块
vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in mounted hook: "ReferenceError: axios is not defined"
...
Vue.use({
install (Vue) {
Vue.prototype.$api = axios.create({
baseURL: 'PUT A BASE URL IF YOU WANT'
})
}
})