我有一个电子(1.7.10)应用程序,它报告它找不到我的ASAR中的7个PNG文件中的5个。所有7个PNG都在同一个文件夹中,其中2个显示在屏幕上。另外5份报告net::ERR_FILE_NOT_FOUND.
img标记的所有src属性都是动态生成的,并使用相对路径(assets/images/MyImage.png)。如果提取ASAR,就可以在正确的文件夹中看到其中的文件(由src属性引用)。
如果我使用控制台将浏览器的位置设置为其中一个图像(document.location.href = "file:///path/to/app.asar/dist/assets/images/MyImage.png"),我将得到相同的结果--7中的2显示OK。
在打包我的应用程序(用电子生成器)之前,所有的图像都会正确显示.
发布于 2019-06-10 13:52:39
让我猜猜,您正在构建一个使用react路由器和BrowserRouter的react?
如果是这样,则使用HashRouter代替。默认情况下,电子不适用于SPA的路由,因为SPA路由会发生变化,但是资源路径总是相对于index.html。
发布于 2021-09-21 14:57:36
我还没有评估其他答案,但对于我的特殊情况,一个非常有效的解决方案。我不相信这是一个很好的文档,所以人们仍然会遇到这个问题,这可能是相当普遍的。关于我的具体情况,这里确定了相关的问题和解决方案。
要解决这个问题,请将<base href='./' />添加到index.html (或者您的起始html文件是承载SPA的任何文件)。这是我的一个完整的例子:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="./" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<meta
http-equiv="Content-Security-Policy"
content="script-src 'Self' 'unsafe-inline';"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>发布于 2018-04-06 05:32:22
const path = require('path');
path.join(__dirname, 'assets/images/MyImage.png');https://stackoverflow.com/questions/49614896
复制相似问题