我正在尝试在我的投资组合中插入图像,但由于某种原因,标签返回 404 并且下一个图像返回错误
The requested resource isn't a valid image for /myImage.jpg received text/html
这是代码块:
import Image from "next/image";
import { ReactElement } from "react";
const MyImage = ():ReactElement => {
return (
<Image
fill
src='/myImage.jpg'
alt="MyImage"
/>
)
}
export default MyImage
Image
组件对于处理图像源非常严格。
您得到
404
,这意味着 not found
或 invalid image
。
myImage.jpg
正确放置在 public
文件夹中。received text/html
此错误意味着您正在传递 html
,或者图像可能无法正确提供,这可能会返回 html
错误页面。请确保该文件确实存在于您指定的路径中,并仔细检查这是否是有效的图像。next.config.js
文件以允许该域用于外部图像。在你的
next.config.js
:
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'domainOfSiteYouHostedYourImage.com',
port: '',
pathname: '/**',
},
],
},
}
希望这有效并解决您的问题