简体   繁体   English

您可以在Electron中使用绝对路径吗?

[英]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.

相关问题 无法使用绝对路径进行导入? - Can't use absolute paths for import? 在 Jest 中使用绝对路径 - Use absolute paths with Jest Typescript / Electron / Webpack模块如何/为什么有绝对路径? - How/Why do Typescript/Electron/Webpack modules have absolute paths? 我们如何只在某些路径中使用 electron.protocol.interceptFileProtocol,而不影响其他请求? - How can we use electron.protocol.interceptFileProtocol with only certain paths, leaving other requests untouched? 如何使用java,html或javascript将相对href属性转换为绝对路径? - How can I use java,html or javascript to convert relative href attributes into absolute paths? 如何使用javascript将相对href属性转换为绝对路径? - How can I use javascript to convert relative href attributes into absolute paths? 使用angular 2 CLI,如何使用绝对路径,所以不必使用来自&#39;../../../shared/thing&#39;的import {..} - using angular 2 CLI, how can I use absolute paths so I don't have to use import { .. } from '../../../shared/thing' FileError {code:5}尝试在Meteor 1.4 Cordova中使用绝对文件路径 - FileError {code: 5} trying to use absolute file paths with Meteor 1.4 Cordova 如何在导入JavaScript时在QML中使用绝对路径? - How to use absolute paths in QML when importing JavaScript? jQuery如何用于使所有相对路径成为绝对路径? - How can jQuery be used to make all relative paths absolute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM