我在Next.JS中遇到了让组件通过props 接受图像源的问题.我传递的是位于项目公共文件夹中的名为"logo.jpg"的图像的路径.当作为另一个组件的props 传递时,它接受图像并正确使用它.当它被用作问题组件的props 时,它甚至无法识别路径是否正在被读取.
我如何解决这个问题,以便Next.JS识别并使用馈送的图像的路径作为props ?
控制台产生的错误
Image is missing required "src" property:
<img alt="" src="data:image/gif;base64,R0…AAAALAAAAAABAAEAAAIBRAA7" decoding="async" data-nimg="intrinsic" style="position:absolute;top:0;…ht:100%;max-height:100%">
(相关)文件 struct
card-page
├───components
│ card.js
│ cardlink.js
├───pages
│ _app.js
│ Index.js
└───public
logo.jpg
工作组件(card.js)
import React from 'react'
import Image from 'next/image'
import styles from '../styles/card.module.css'
export default function Card( {title, image, alt, description} ) {
console.log(image)
return(
<div className={styles.container}>
<Image
src={image}
width="200"
height="200"
alt={alt} />
<h1>{title}</h1>
<p>{description}</p>
</div>
)
}
组件损坏(cardlink.js)
import Image from 'next/image'
import Link from 'next/link'
import styles from '../styles/cardlink.module.css'
export default function CardLink( text, image, href ) {
console.log(image)
return(
<div className={styles.container}>
<Image
src={image}
width={64}
height={64}
alt="" />
</div>
)
}
Index.js
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/home.module.css'
import CardLink from '../components/cardlink'
import Card from '../components/card'
export default function Home() {
return (
<div className={styles.container}>
<Card
image="/logo.jpg" />
<CardLink
image="/logo.jpg"
text="Link 1"
href=""
alt="what"
/>
</div>
)
}