vue下跨域设置

一、在使用vue开发的时候常常要涉及到跨域的问题,其实在vue cli中是有咱们设置跨域请求的文件的。css

二、当跨域没法请求的时候咱们能够修改工程下config文件夹下的index.js中的dev:{}部分。
dev: {vue

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设置为咱们须要访问的域名。webpack

三、而后在main.js中设置全局属性:git

Vue.prototype.HOST = '/api'

四、至此,咱们就能够在全局使用这个域名了,以下:github

var url = this.HOST + '/movie/in_theaters'
    this.$http.get(url).then(res => {
      this.movieList = res.data.subjects;
    },res => {
      console.info('调用失败');
    });
相关文章
相关标签/搜索