electron常见问题
# 构建时出现 is not supported since 3.0 anymore. Please move 'build' into the development package.json
把 build
从 package.json
中移除,在 vue.config.js
中写
module.exports = {
runtimeCompiler: true,
pluginOptions: {
electronBuilder: {
builderOptions: {
// build配置在此处
// options placed here will be merged with default configuration and passed to electron-builder
}
},
},
};
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 生产环境下出现AxiosError
webPreferences: {
webSecurity: false // 添加此属性
}
1
2
3
2
3
# Electron Vue 打包后视频无法播放
background.js
加上
const path = require("path");
function registerLocalVideoProtocol() {
protocol.registerFileProtocol("local-video", (request, callback) => {
const url = request.url.replace(/^local-video:\/\//, "");
// Decode URL to prevent errors when loading filenames with UTF-8 chars or chars like "#"
const decodedUrl = decodeURI(url); // Needed in case URL contains spaces
try {
// eslint-disable-next-line no-undef
return callback(path.join(__static, decodedUrl));
} catch (error) {
console.error(
"ERROR: registerLocalVideoProtocol: Could not get file path:",
error
);
}
});
}
app.on("ready", async () => {
registerLocalVideoProtocol();
......
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
然后视频文件放在public
文件夹下
引用视频 src="local-video://video1.mp4"
# 页面中出现 require is not defined 或者 __dirname is not defined
配置nodeIntegration
和 contextIsolation
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences:{
nodeIntegration: true,
contextIsolation: false
}
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Cannot read property 'existsSync' of undefined
显示的是fs
的existsSync
方法不存在
页面中引用使用window.require
代替require
const {ipcRender} = window.require('electron')
1
上次更新: 2023/09/22, 16:54:32