1. 程式人生 > >axios 在 webpack 打包報錯:Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined

axios 在 webpack 打包報錯:Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined

解決方法:

import axios from 'axios'

// 在這裡不能使用 Vue.use(axios)
// Vue.use(axios)

Vue.prototype.$http = axios

模組引用:

                // 基於vue的形式
                var form = new FormData();
                form.append('name', this.name);
                form.append('phone', this.phone);
                form.
append('sex', this.sex); form.append('photo', this.img); form.append('user_address', this.address); let config = { headers: {'Content-Type': 'multipart/form-data'} }; //新增請求頭 this.$http.post('url', form,
config) .then(response => { console.log(response.data); })

而對於 vue-resource:

import VueResource from 'vue-resource';
Vue.use(VueResource);