[英]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? 有没有办法对电子使用绝对路径进行依赖(例如
<script src="/my-script.js"></script>
)并使其起作用?
Currently it just does mainWindow.loadURL('file://' + __dirname + '/index.html');
目前,它只执行
mainWindow.loadURL('file://' + __dirname + '/index.html');
Which loads index.html just fine, but here's the thing, index.html loads 加载index.html很好,但是这里就是index.html加载的东西
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/ ). 这会使我的生活变得轻松很多,否则我将不得不重构一堆模板URL,并且如果我想在网站的子目录中添加页面(例如, http:/) ,做一些操作将永远破坏我的应用程序。 /website.com/m/ )。
Any suggestions? 有什么建议么? Thanks!
谢谢!
Relative paths work just fine.. Try removing the first /
like so: <script src="my-script.js"></script>
相对路径也可以正常工作。尝试删除第一个
/
像这样: <script src="my-script.js"></script>
If you really need access the your apps absolute path you can get it with app.getAppPath()
: 如果您确实需要访问您的应用程序的绝对路径,则可以使用
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. 您可以通过使用少量嵌入式JS将脚本注入DOM来加载脚本。
您可以使用protocol.interceptFileProcotol()
将内置URI覆盖为文件路径解析,然后您的处理程序可以将/my-script.js
映射到所需的任何路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.