Vue使用代理请求后端接口

//修改config/index.js
    
    proxyTable: {
      '/demo': {
        target: "https://blog.phpfs.com",
        changeOrigin: true,
        // pathRewrite: {
        //   '^/': '/'
        // }
      }

    },
//使用
 mounted () {
    var _this = this
    axios.get('/demo/demo.json').then(response => {
      console.log('返回数据', response.data)
      if (response.data.code === 0) {
        _this.$data.bannerItems = response.data.data.bannerItems
      }
    }).catch(error => console.log(error))
  },
//实际请求的接口地址是
https://blog.phpfs.com/demo/demo.json

PS:配置之后执行yarn run dev即可生效

Bookmark the permalink.

Comments are closed.