electron 打包vue 请求file协议纠正

并未找到直接解决方法,最后直接在axios拦截器里解决

//axios请求拦截
axios.interceptors.request.use(
  config => {
    //...
    if (!config.url.includes('http')) {
      config.url = process.env.BASE_URL + config.url  //补上你的请求路径
    }
    //...
    return config;
  },
  err => {
    return Promise.reject(err)
  }
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Electron 中,当你将应用程序打包后,你可以使用 file 协议请求本地文件。下面是一个简单的示例代码,演示了如何在 Electron请求本地文件: ```javascript const { app, BrowserWindow } = require('electron'); const path = require('path'); const url = require('url'); let mainWindow; function createWindow() { mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }); // 加载 index.html 文件 mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })); // 其他窗口关闭时触发 mainWindow.on('closed', function () { mainWindow = null; }); } // 应用程序就绪后创建窗口 app.on('ready', createWindow); // 其他窗口全部关闭时退出应用程序 app.on('window-all-closed', function () { // 在 macOS 上,应用程序及其菜单栏通常保持激活状态,直到用户通过 Cmd + Q 显式退出 if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', function () { // 在 macOS 上,点击 dock 图标并且没有其他窗口打开时,重新创建一个窗口 if (mainWindow === null) { createWindow(); } }); ``` 在上面的代码中,`mainWindow.loadURL()` 方法用于加载 `index.html` 文件。`pathname` 参数指定了文件的路径,`protocol` 参数设置为 `'file:'`,这样就可以使用 file 协议来加载本地文件。 请确保在 `webPreferences` 中将 `nodeIntegration` 设置为 `true`,以便在渲染进程中使用 Node.js API,例如读取文件。同时也要注意,加载本地文件可能存在安全风险,因此请谨慎处理用户输入和文件访问权限。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值