[英]Can't load an image inside my React Component
Not sure what I'm doing wrong, but this image shows up broken when I render the page. 不确定我做错了什么,但是在渲染页面时此图像显示为损坏。
Project Directory Structure is: 项目目录结构为:
root
src
client
components
myComponent.js
ext
assets
logo.js
myComponent.js myComponent.js
import {Component,Store} from 'reactivate';
const CompanyDetail = Component({
store: Store('./company'),
render(){
return (
<div className='ft-companyDetail'>
<div id="header">
<div class="panel vertical-space">
<img src="https://placeholdit.imgix.net/~text?txtsize=20&txt=1650%C3%97300&w=1650&h=300" />
</div>
<div id="logo" class="panel vertical-space">
<img src="../../ext/assets/logo.png" />
</div>
</div>
</div>
)
}
})
export default CompanyDetail;
server.js server.js
var express = require('express'),
app = module.exports = express(),
port = 3000;
app.use(express.static('client'));
app.use(function (req, res, next) {
res.send('Sorry, Page Not Found');
});
app.listen(port, function () {
console.log('Ready on port %d', port);
}).on('error', function (err) {
console.log(err);
});
When the page renders, I get a broken image. 页面呈现时,我得到了损坏的图像。 I assume you should be able to load images like normal with and I've tried to troubleshoot the image path with no luck.
我认为您应该能够像正常情况一样加载图像,并且我尝试了在没有运气的情况下对图像路径进行故障排除。
From your code you are serving static files from client folder. 通过您的代码,您可以从客户端文件夹提供静态文件。
app.use(express.static('client'));
So your path localhost:3000/ext/assets/logo.png is failing. 因此,您的路径localhost:3000 / ext / assets / logo.png失败了。 Hopefully you will get the same error when you try to load the above url from your browser.
希望当您尝试从浏览器加载上面的URL时,也会遇到同样的错误。
You may need to move the ext folder inside client folder. 您可能需要将ext文件夹移到客户端文件夹中。
The better approach is to use Webpack. 更好的方法是使用Webpack。 https://www.codementor.io/reactjs/tutorial/beginner-guide-setup-reactjs-environment-npm-babel-6-webpack
https://www.codementor.io/reactjs/tutorial/beginner-guide-setup-reactjs-environment-npm-babel-6-webpack
It looks like your server root is /src
and is not serving the ext directory. 看来您的服务器根目录是
/src
,并且不在ext目录中。 Your have to use the ext directory in your express server config 您必须在快速服务器配置中使用ext目录
var path = require('path');
app.use('/ext', express.static(path.resolve(__dirname, '../ext')));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.