javascript – So konfigurieren Sie vue-cli ProxyTable
高洛峰
高洛峰 2017-05-16 13:39:25
0
3
897

Wie verwende ich Setting.host + '/api/sop/' in der Online-Umgebung und der lokale Entwickler fordert localhost:3000 an?

const instance = axios.create({
  baseURL: setting.host + '/api/sop/',
  timeout: 20000,
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
});

config


  proxyTable: {
  '/api': {
    target: "http://127.0.0.1:3000",
    changeOrigin: true,
    pathRewrite: {
      '^/api': ""
    }
  }
},
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

Antworte allen(3)
世界只因有你

用的vue-resource,理论上思路是一样的。proxyTablenginx的反向代理是一样的道理,拦截特定的url,转发到其他服务器。

// config
proxyTable: {
  '/api': {
    target: 'http://10.0.0.10:8080',
    changeOrigin: true,
    pathRewrite: {
      '^/api': '/api'
    }
  }
}

// code
this.$http.post('/api/login',{
  username: 'xxx',
  password: 'xxx'
}).then((response) => {
  // ...
}, (response) => {
  // ...
});

# 生产环境 nginx
location /api {
  proxy_pass http://10.0.0.10:8080/api;
}
迷茫

可以配置一个环境变量,通过判断环境变量确定使用哪一种配置

process.NODE_ENV === 'LOCAL' ? proxyTableLocal : proxyTableServer
phpcn_u1582

设置后, npn run dev阶段, 本地如果访问'/get/apple, 本地服务器会帮你访问http://api.com:6688/get/apple拿到远程的数据, 变相的实现了跨域功能

打开config/index.js, 添加proxyTable属性

module.exports = {

build: {...}
dev: {
       ...
    proxyTable: {
        '/': {
                 target: 'http://api.com:6688',
                 changeOrigin: true
            }
    },
       ...
}

}

https://github.com/383514580/...

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!