Vue 2.0 跨域解决方案
2018-03-28 14:52
477 查看
1、在使用vue开发的时候经常要涉及到跨域的问题,其实在vue cli中是有我们设置跨域请求的文件的。2、当跨域无法请求的时候我们可以修改工程下config文件夹下的index.js中的dev:{}部分。dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: false,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://api.douban.com/v2',
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
}将target设置为我们需要访问的域名。3、然后在main.js中设置全局属性:Vue.prototype.HOST = '/api'4、至此,我们就可以在全局使用这个域名了,如下:var url = this.HOST + '/movie/in_theaters'
this.$http.get(url).then(res => {
this.movieList = res.data.subjects;
},res => {
console.info('调用失败');
});
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: false,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://api.douban.com/v2',
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
}将target设置为我们需要访问的域名。3、然后在main.js中设置全局属性:Vue.prototype.HOST = '/api'4、至此,我们就可以在全局使用这个域名了,如下:var url = this.HOST + '/movie/in_theaters'
this.$http.get(url).then(res => {
this.movieList = res.data.subjects;
},res => {
console.info('调用失败');
});
相关文章推荐
- vue2.0 项目build后资源文件报错404的解决方案
- vue2.0 本地跨域处理
- vue开发过程中跨域最简单解决方案
- Vue2.0用户权限控制解决方案的示例
- asp.net web api2.0 ajax跨域解决方案
- Vue2.0用户权限控制解决方案
- Vue2.0 之 自带浏览器里无法打开(兼容IE处理) - 解决方案 命令
- asp.net web api2.0 ajax跨域解决方案
- Vue2.0用户权限控制解决方案
- vue2.0 axios跨域并渲染的问题解决方法
- vue2.0设置proxyTable使用axios进行跨域请求的方法
- Vue2.0 vue-source jsonp 跨域请求
- vue2.0 axios跨域并渲染问题解决
- vue2.0 通过v-html指令渲染的富文本无法修改样式的解决方案
- 详解vuelidate 对于vueJs2.0的验证解决方案
- Vue.js 2.0 跨域请求数据
- Vue2.0 之 自带浏览器里无法打开(兼容处理) - 解决方案
- Vuejs2.0之异步跨域请求——axios
- Vue2.0利用 v-model 实现组件props双向绑定的优美解决方案