Can you use absolute paths in Electron?

Simple question here that I can't seem to find a straight answer for.

Is there any way to use absolute paths for dependencies (eg <script src="/my-script.js"></script> ) with electron and have it work?

Currently it just does mainWindow.loadURL('file://' + __dirname + '/index.html');

Which loads index.html just fine, but here's the thing, index.html loads

Which fails because it looks in the root of the entire hard drive

This would make my life quite a bit easier, as otherwise i'd have to refactor a bunch of template URLs, and doing some would forever break my app if I ever wanted to add pages in a subdirectory on the website (eg http://website.com/m/).

Any suggestions? Thanks!


您可以使用protocol.interceptFileProcotol()覆盖内置URI来解析文件路径,然后您的处理程序可以将/my-script.js映射到您想要的任何路径。


Relative paths work just fine.. Try removing the first / like so: <script src="my-script.js"></script>

If you really need access the your apps absolute path you can get it with app.getAppPath() :

var app = require('remote').require('app');
console.log(app.getAppPath());

And you could load scripts by injecting them into the DOM with a small amount of inline JS.

链接地址: http://www.djcxy.com/p/90406.html

上一篇: 如何从规范化表格中获得最受欢迎的标签?

下一篇: 你可以在Electron中使用绝对路径吗?