我将尝试使用webpack-dev-server代理配置将API请求发送到外部域名,但似乎无法使其正常工作。
这是我的配置:
所以,每当我的应用程序使用uri
在我的Vue应用程序中,我正在使用
URL已经正确构建,但它们仍然指向webpack-dev-server上的localhost:8080。 我阅读了webpack-dev-server的文档,但无法确定我的设置是否正确。有任何想法吗?
这是我的配置:
var path = require('path')
module.exports = {
entry: './client/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public/assets'),
publicPath: 'assets'
},
devServer: {
contentBase: 'public',
proxy:{
'/api/v1*': {
target: 'http://laravelandwebpack.demo/',
secure: false
}
}
}
}
所以,每当我的应用程序使用uri
/api/v1...
发出请求时,它应该将该请求发送到http://laravelandwebpack.demo
。在我的Vue应用程序中,我正在使用
vue-resource
来发出请求,并将所有请求默认为所需的uri前缀:var Vue = require('vue')
Vue.use(require('vue-resource'))
new Vue({
el: 'body',
http: {
root: '/api/v1', // prefix all requests with this
headers:{
test: 'testheader'
}
},
ready: function (){
this.$http({
url: 'tasks',
method: 'GET'
}).then(function (response){
console.log(response);
}, function (response){
console.error(response);
})
}
})
URL已经正确构建,但它们仍然指向webpack-dev-server上的localhost:8080。 我阅读了webpack-dev-server的文档,但无法确定我的设置是否正确。有任何想法吗?
localhost:8080
,因为那是您的开发服务器,只有请求到达那里后,开发服务器才会代理它(即将其发送到)您提供的外部URL。开发工具永远不会注意到代理。您的请求会发生什么?promise.catch()是否记录了错误?这是什么?(您不会碰巧使用vue-cli的webpack模板吗?) - Linus Borg