VUE代理方式解决跨域问题

在config文件夹中的index.js设置pxoxyTable

dev: {

    // Paths
    assetsSubDirectory: 'assets',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://localhost:8090/',//设置你调用的接口域名和端口号 别忘了加http
        changeOrigin: true,
        pathRewrite: {
            '^/api': '' // 这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://localhost:8090/users',直接写‘/api/users’即可
        }
    }
    },
    ......

在组建中结合vue-resource或者axios使用即可

 methods: {
    dataGet() {
      this.$http
        .get('/api/users', {
          params: {},
          headers: {
            token: 'a'
          }
        })
        .then(
          res => {
            console.info(res.data)
          },
          error => {
            console.info(error)
          }
        )
    },
    ......

你可能感兴趣的:(VUE代理方式解决跨域问题)