简体   繁体   English

为什么我的 CSS 背景图像没有加载到电子中?

[英]Why are my CSS background images not loading in electron?

I'm developing a react app packaged in electron that has to download it's content from the internet.我正在开发一个打包在电子中的反应应用程序,它必须从互联网上下载它的内容。 That content then gets stored in the data folder.然后将该内容存储在数据文件夹中。 Most of the time this works just fine.大多数时候这工作得很好。 I have to pay attention to set my URLs properly, or it breaks when I move between Windows and Mac, but with a little time and attention I can make it work on both.我必须注意正确设置我的 URL,否则当我在 Windows 和 Mac 之间移动时它会中断,但只要花一点时间和注意力,我就可以让它在两者上都能正常工作。

All that came to a crashing halt just now, because I need to use an image as a background.刚才所有的一切都戛然而止,因为我需要使用图像作为背景。 My code works 100% fine on Mac, breaks on windows.我的代码在 Mac 上 100% 正常工作,在 Windows 上中断。 I've validated the output of my code, it's producing a valid path to the relevant file.我已经验证了我的代码的输出,它生成了相关文件的有效路径。 I can even copy & paste that into the browser and the background image loads just fine.我什至可以将其复制并粘贴到浏览器中,背景图像加载得很好。 For some reason, the css code is mangling the results -- and I really do mean 'mangling', stripping slashes, creating weird escapes, inserting garbage characters in strange places.出于某种原因,css 代码正在修改结果——我的意思是“修改”,剥离斜杠,创建奇怪的转义,在奇怪的地方插入垃圾字符。 When I use the debugger I can see both my console.log() outputs with a valid url, and a 404 error when the browser tries to get another url entirely.当我使用调试器时,我可以看到带有有效 url 的console.log()输出,以及当浏览器尝试完全获取另一个 url 时的 404 错误。 You can see that it started out as the target URL, but it's been transformed in ways I don't understand.您可以看到它最初是作为目标 URL,但它以我不理解的方式进行了转换。

The following code is vastly simplified -- it doesn't include, for example, the actually directory structure of my app in the path.join() call.下面的代码大大简化了——例如,它不包括我的应用程序在 path.join() 调用中的实际目录结构。

var fullPath = path.join(AppDataFolder, 'stuff', filename)
var url = url.format({ pathname: fullPath, protocol: 'file', slashes: true })
//Url, if printed, is file:///c:\app\data\dir\rental\file.suffix -- the right output
<div style={{ backgroundImage: `url(${url})` }}>
</div>
//Use the debugger on the result above, and you get an error 404ing on:
//file:///C:/appdatadir%E3%89%83%0garbagefilesuffix

If I instead put the target into an image tag, <img src={target} /> , it works fine, both Windows & Mac.如果我将目标放入图像标签<img src={target} /> ,它在 Windows 和 Mac 上都可以正常工作。

What am I doing wrong?我究竟做错了什么? How do I fix this?我该如何解决?

In reactjs we have supposed to be use CSS properties in Camel case.在 reactjs 中,我们应该在 Camel 案例中使用 CSS 属性。

<div style={{ backgroundImage: `url(${url})` }}>

Reference: https://reactjs.org/docs/dom-elements.html#style参考: https : //reactjs.org/docs/dom-elements.html#style

We need more information to see if the url encode-ish issue really the problem you have.我们需要更多信息来查看 url 编码问题是否真的是您遇到的问题。 Would you try to put a double quote like你会试着加一个双引号吗

<div style={{ backgroundImage: `url("${url}")` }}>

to see if that solve your problem?看看能不能解决你的问题?

and for not working is the image not show up for you or the network tab of the browser really show it ping that odd URL and return a 404?对于不工作是图像没有显示给您还是浏览器的网络选项卡真的显示它ping那个奇怪的URL并返回404? as you may simply CSS issue like width and height needed.因为您可能只是 CSS 问题,例如需要的宽度和高度。

<div style={{ backgroundImage: `url("${url}")`, width: 40, height:40 }}>

I think you mentioned you simplified your code so it may not be the issue but just checking... I have a mac here so referring image to the file system is different than in PC so won't be able to reproduce that same issue.我想你提到你简化了你的代码,所以它可能不是问题,而只是检查......我在这里有一台 mac,所以引用文件系统的图像与 PC 中的不同,因此将无法重现相同的问题。 But the one more thing you may want to check is the permission issue as not sure what hosting your react app (node?) that may have issue to talk to local file.但是,您可能还想检查的另一件事是权限问题,因为不确定托管您的 React 应用程序(节点?)的内容可能与本地文件对话有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM