简体   繁体   English

使用“serve -s build”来提供反应应用程序时,静态 img 不显示

[英]When using "serve -s build" to serve react app, static img does not show up

first time poster, so please let me know what other information I can give to help.第一次海报,所以请让我知道我可以提供哪些其他信息来提供帮助。 I've been going in circles with a strange react issue.我一直在绕一个奇怪的反应问题。

The Problem问题

Here's a screenshot of the problem这是问题的截图

As you can see in the image attached, even though the console inspect element's tab shows that the img src is being read properly (the img is even shown on hover of the src in the console), for some reason the image isn't showing up where it is highlighted on the dom.正如您在附加的图像中看到的那样,即使控制台检查元素的选项卡显示 img src 正在被正确读取(img 甚至显示在控制台中 src 的悬停上),但由于某种原因图像没有显示在 dom 上突出显示的位置。

This happens when I run npm start with "serve -s build" as the configuration.当我以“serve -s build”作为配置运行 npm start 时会发生这种情况。 When I execute "npm start" with the original "react-scripts start", the imgs show up properly.当我使用原始的“react-scripts start”执行“npm start”时,imgs 正确显示。

Another strange thing is that, in the screenshot above, I am able to display the desired image above those cards.另一个奇怪的事情是,在上面的屏幕截图中,我能够在这些卡片上方显示所需的图像。 So it is being served to the dom correctly, but somehow serve is messing up my css for the images that should show up on the cards?所以它被正确地提供给 dom,但不知何故,为应该显示在卡片上的图像而弄乱了我的 css?

I've ran 'npm run-script build' as well before running npm start, but the same thing happens.在运行 npm start 之前,我也运行了 'npm run-script build',但同样的事情发生了。

package.json包.json

 "dependencies": {
    "react-scripts": "^3.3.0",
    "serve": "^11.3.0",
  },
  "scripts": {
    "start": "serve -s build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
  },

Skills.js技能.js

import './Skills.css';
import frontEndImage from '../../images/front-end.png';
import languagesImage from '../../images/languages.png';
import backendImage from '../../images/backend.png';
import miscellaneousImage from '../../images/miscellaneous.png';
let Skills = () => {

return (
    <div className="container">
        <h1 className="subheader lh-title pb5">Skills</h1>
        <div className="ml4 flex flex-wrap justify-center">
            <img src={backendImage} alt="header"/>
            <div className="skill-card card">
                <img src={backendImage} alt="header"/>
                <div className="experience-content">
                    <h1> Back End </h1>
                    {backSkill}
                </div>
            </div>
        </div>
    </div>
)

Skills.css技能.css

.card img {
opacity: 80%;
z-index: 100;
}

.skill-card {
height: 550px;
}

I can't find a lot of documentation for the serve package, so I can't understand why the static images are not being served properly.我找不到很多关于 serve 包的文档,所以我不明白为什么不能正确提供静态图像。 Anyone have an idea?有人有想法吗? Thanks ahead of time!提前致谢!

Background - Why am I even using npm serve package?背景 - 为什么我什至使用 npm serve 包?

When I accessed https://localhost:3000 , I would get a security "React app error: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS".当我访问https://localhost:3000 时,我会得到一个安全信息“React 应用程序错误:无法构建‘WebSocket’:可能无法从通过 HTTPS 加载的页面启动不安全的 WebSocket 连接”。 Same error throws when I deploy it to heroku, and access with https.当我将其部署到 heroku 并使用 https 访问时,会引发相同的错误。 I followed the solutions listed at this stackoverflow issue.我遵循了这个 stackoverflow 问题中列出的解决方案 and installing serve package and changing the package.json was the only solution that I could find for that problem.安装服务包并更改 package.json 是我为该问题找到的唯一解决方案。

So after 2 months of avoiding this problem, I just found out that it's a simple CSS fix.所以在避免这个问题 2 个月之后,我才发现这是一个简单的 CSS 修复。

I'm not entirely sure why, but whenever I built the project, whether it was with "npm react-scripts build" or "npm run-scripts build", it somehow toggles my opacity to 1%.我不完全确定为什么,但是每当我构建项目时,无论是使用“npm react-scripts build”还是“npm run-scripts build”,它都会以某种方式将我的不透明度切换为 1%。 It took me so long just to inspect the element's CSS because I was not expecting it to change the CSS properties from a build.我花了很长时间来检查元素的 CSS,因为我没想到它会从构建中更改 CSS 属性。

My CSS for the images that were not showing up looked like this:我的未显示图像的 CSS 如下所示:

.card img { opacity: 90%; .card img { 不透明度:90%; z-index: 100; z-索引:100; } }

Very simple, but after the build it looked like this:非常简单,但构建后它看起来像这样:

.card img { opacity: 1%; .card img { 不透明度:1%; z-index: 100; z-索引:100; } }

The way I solved this was just to remove the opacity property from .card img css entirely.我解决这个问题的方法只是从 .card img css 中完全删除 opacity 属性。 If I use the opacity property at all, it would just toggle to 1% on build.如果我完全使用 opacity 属性,它只会在构建时切换到 1%。

.card img { z-index: 100; .card img { z-index: 100; } }

I'm not sure why this is happening, but glad it's fixed.我不确定为什么会发生这种情况,但很高兴它已修复。

percentages is not valid value for opacity and build script fix it (I agree to transform it to 1% is not the best way but anyway) just don't use percentages, use value from 0.0 - 1.0 as it is described here .百分比是不是不透明度和构建脚本修复有效值(我同意把它转换为1%,是不是最好的方式,但无论如何)只是不使用百分比,使用值从0.0 -因为它是描述1.0在这里

So instead of所以代替

.card img { opacity: 90%; .card img { 不透明度:90%; z-index: 100; z-索引:100; } }

You must have你必须有

.card img { opacity: 0.9; .card img { 不透明度:0.9; z-index: 100; z-索引:100; } }

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

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