跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 后端初学 vue3+vite 快被这个打包+跨域问题烦死了!

发表于

问题是这样的:

我使用了 vue3+vite 开发,vite 配置了代理如下:

proxy: {
      '^/api*': {
        target: 'http://localhost:9961/',
        changeOrigin: true,
        // 替换掉/api 路径
        rewrite: (path) => path.replace(/^\/api/, ''),
      }
    }

以上在本地开发时一切很美好,都正常,但是! 当打包成静态资源部署上服务器后恶心的就来了 vite 的代理失效了,访问路径变成了前端页面路径+/api/xxx ,查了一下这种情况需要使用 nginx 代理/api 路径,然后转发到后台服务。

但是啊,我做的这个东西部署条件不支持装 nginx 啊,就不能直接访问后端接口吗?我后端接口都已经配置好了允许跨域,不管我是写死路径还是删除 vite 的 proxy 配置,最终打包后都会给我把请求路径替换成 /api/xxx

我只想要直接请求后端接口为啥这么难?

还有个让我很不解的地方,为啥用npm run preview一切都正常,我配好的后端接口地址也会正常请求,一旦放到服务器上就又变成了/api/xxx

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.