Vue的跨域问题可以使用代理的方式解决,在项目config文件夹中的index.js中配置proxyTable选项。
config > index.js
- dev: {
- env: require('./dev.env'),
- port: 8080,
- autoOpenBrowser: true,
- assetsSubDirectory: 'static',
- assetsPublicPath: '/',
- proxyTable: {
- '/api': {
- target: 'http://open.douyucdn.cn/api/RoomApi',
- changeOrigin: true,
- pathRewrite: {
- '^/api': ''
- }
- }
- },
- // CSS Sourcemaps off by default because relative paths are "buggy"
- // with this option, according to the CSS-Loader README
- // (https://github.com/webpack/css-loader#sourcemaps)
- // In our experience, they generally work as expected,
- // just be aware of this issue when enabling this option.
- cssSourceMap: false
- }
在项目中使用 vue-resource 发起请求
- this.$http.get('/api').then((response)=>{
- // 响应成功回调
- console.log(response);
- },(error)=>{
- // 响应错误回调
- console.log(error);
- })