欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue下跨域设置的相关介绍

程序员文章站 2022-09-08 22:06:02
本文介绍了vue下跨域设置的相关介绍,分享给大家,具体如下: 1、在使用vue开发的时候经常要涉及到跨域的问题,其实在vue cli中是有我们设置跨域请求的文件的。...

本文介绍了vue下跨域设置的相关介绍,分享给大家,具体如下:

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('调用失败');
  });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。