Javascript 图像不可见(无法识别源)
我正在React.js+Spring-in-back中制作一个简单的web应用程序 在函数displayItems中从本地路径显示photo.img时出现问题。图片不可见。如果我以相同的代码src=http从web加载文件。。。。。。。一切都很好 你能帮忙吗Javascript 图像不可见(无法识别源),javascript,image,reactjs,infinite-scroll,Javascript,Image,Reactjs,Infinite Scroll,我正在React.js+Spring-in-back中制作一个简单的web应用程序 在函数displayItems中从本地路径显示photo.img时出现问题。图片不可见。如果我以相同的代码src=http从web加载文件。。。。。。。一切都很好 你能帮忙吗 import React, { Component } from 'react'; import '../index.css'; class Author extends Component { constructor(props) {
import React, { Component } from 'react';
import '../index.css';
class Author extends Component {
constructor(props) {
super(props);
this.state = {
mail: window.location.href.slice(32, -7),
items: 2,
loadingState: false
};
this.handleChange = this.handleChange.bind(this);
}
componentDidMount() {
this.refs.iScroll.addEventListener("scroll", () => {
if (this.refs.iScroll.scrollTop + this.refs.iScroll.clientHeight >=this.refs.iScroll.scrollHeight){
this.loadMoreItems();
}
});
}
displayItems() {
var items = [];
for (let i = 0; i < this.state.items; i++) {
//PROBLEM
items.push(<img src="../resources/Photos/1.jpg"></img>);
}
return items;
}
loadMoreItems() {
this.setState({ loadingState: true });
setTimeout(() => {
this.setState({ items: this.state.items + 2, loadingState: false });
}, 3000);
}
render() {
return (
<div
className="vc"
ref="iScroll"
style={{ height: "200px", overflow: "auto" }}
>
<h2>My adventures: </h2>
<div>
{this.displayItems()}
</div>
{this.state.loadingState
? <p className="loading">
loading More Images..
</p>
: ""}
</div>
);
}
}
export default Author;
您必须使用require或import获取图像,然后在src中使用它
const image = require("../resources/Photos/1.jpg")
...
items.push(<img src={image}></img>);
你的一般方法看起来不错。您是否可以查看浏览器的调试器,并查看当img标记出现在页面上时,它的HTML是什么?下面是一个演示示例:我认为问题在于图像的位置。您将它们与相对路径一起使用,而不导入或要求它们。然后编译器不知道应该将它们添加到根束中。您可以尝试移动到您的公用文件夹。thx,您是wright。你知道为什么吗:const image=require../resources/Photos/1.jpg,但const image=require../resources/Photos/+i+.jpg;不是吗?谢谢,你是赖特。你知道为什么吗:const image=require../resources/Photos/1.jpg,但const image=require../resources/Photos/+i+.jpg;不是吗?很可能是因为如果在初始化i之前放置了语句,那么我没有值