vue + tauri 打包后路由变成了tauri.localhost?

将vue发到tauri上npm run tauri dev启动后的后台api的请求地址是http://ip:port/xxx

为啥npm run tauri build打包后变成了http://tauri.localhost:port/xxx

tauri.conf.json

"tauri": {
    "allowlist": {
      "all": true,
      "http": {
        "all": true,
        "request": true,
        "scope": [
          "http://192.168.0.101:18088/*"
        ]
      }
    },
    "bundle": {
      "active": true,
      "category": "DeveloperTool",
      "copyright": "",
      "deb": {
        "depends": []
      },
      "externalBin": [],
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/128x128@2x.png",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "identifier": "com.tauri.build",
      "longDescription": "",
      "macOS": {
        "entitlements": null,
        "exceptionDomain": "",
        "frameworks": [],
        "providerShortName": null,
        "signingIdentity": null
      },
      "resources": [],
      "shortDescription": "",
      "targets": "all",
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": ""
      }
    },
    "security": {
      "dangerousUseHttpScheme": true
    },
    "updater": {
      "active": false
    },
    "windows": [
      {
        "fullscreen": false,
        "height": 600,
        "resizable": true,
        "title": "vue-admin-template",
        "width": 1200
      }
    ]
  }
阅读 1.1k
avatarAI BotBETA

这个问题可能是因为 Tauri 在处理路由和 URL 时默认使用了 tauri.localhost 作为其域名。这主要是为了在开发过程中提供便利,因为你可以直接在浏览器中打开你的应用,而不需要进行任何配置。

在你的情况下,你可能希望所有的请求都使用相对路径,即从 /xxx 开始,而不是完整的 http://tauri.localhost:port/xxx

tauri.conf.json 文件中,你应该设置你的路由规则。Tauri 允许你定义一个 routes 对象来自定义你的路由。例如:

"routes": {
  "/api": "/xxx"
}

这将使所有以 /api 开头的请求都被重定向到 /xxx。当然,你需要根据你的实际需求来设置这个对象。例如,如果你需要为不同的后端服务定义不同的路由,你可能需要定义一个更复杂的对象。

注意:这个设置只对开发环境有效,npm run tauri build 构建的生产环境配置会被忽略。在生产环境中,你应该在前端代码中处理 API 请求的 URL。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题