Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue-Cli 项目将本地环境模拟为线上环境 #138

Closed
Dream4ever opened this issue Feb 19, 2021 · 0 comments
Closed

Vue-Cli 项目将本地环境模拟为线上环境 #138

Dream4ever opened this issue Feb 19, 2021 · 0 comments
Labels
Vue Powerful framework

Comments

@Dream4ever
Copy link
Owner

Dream4ever commented Feb 19, 2021

解决过程

开发时运行在本地的前端项目,调用服务器上的用户登录接口,返回的 cookie 因为和本地的域名不一致,所以无法保存,导致其他需要权限的接口都因没有 cookie 而调用失败。

要想将运行在 localhost 上的前端项目,模拟为运行在线上环境 web.abc.com 上的项目,需要进行如下修改。

修改 vue.config.js 文件中的 proxy 字段为类似下面的内容:

    proxy: {
      '/api': {
        target: 'http://api.abc.com/api',
        ws: false,
        changeOrigin: true
      }
    }

然后修改本机的 HOST 文件,加上 127.0.0.1 web.abc.com 这么一行,必须要的话,在修改完 HOST 文件后刷新 DNS。

这样一来,在本地环境访问 web.abc.com,其实访问的是搭建在本地 localhost 的前端项目,同时后端 API 返回的 cookie 也可以正常保存。

参考资料

@Dream4ever Dream4ever added the Vue Powerful framework label Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Vue Powerful framework
Projects
None yet
Development

No branches or pull requests

1 participant