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

请教:如何在开发时,指定API根地址呢? #14

Closed
lfzfrj88 opened this issue Nov 2, 2022 · 3 comments
Closed

请教:如何在开发时,指定API根地址呢? #14

lfzfrj88 opened this issue Nov 2, 2022 · 3 comments

Comments

@lfzfrj88
Copy link

lfzfrj88 commented Nov 2, 2022

请教:如何在开发时,指定API根地址呢?
现在都是从mock走的, 我要从我后台api获取

@iceqing
Copy link
Owner

iceqing commented Nov 2, 2022

vite支持直接配置代理
举个例子,如果是/api开头的都走代理http://127.0.0.1:4444/ (这个换成你的代理即可)
可以在根目录下vite.config.ts配置的server下配置如下:

server: {
        port: 4000,
        host: "localhost",
        open: "/",
        proxy: {
            "/api": {
              target: "http://127.0.0.1:4444/",
              changeOrigin: true,
              rewrite: (path) => path,
            },
        }
    }

@iceqing
Copy link
Owner

iceqing commented Nov 2, 2022

如果是是使用webpack可以直接在build/webpack.config.dev.jsdevServer下配置

proxy: { // 配置代理(只在本地开发有效,上线无效)
      "/api": { // 这是请求接口中要替换的标识
        target: "http://127.0.0.1:4444", // 被替换的目标地址,即把 /api 替换成这个
        pathRewrite: {"^/api" : "/api"}, 
        secure: false, // 若代理的地址是https协议,需要配置这个属性
      }

@iceqing
Copy link
Owner

iceqing commented Nov 8, 2022

issue先关掉,后续有问题的话可以再次打开。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants