我在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>
    )
}

推荐答案

你这里有一个打字错误

以前:

export default function CardLink( text, image, href ) {

之后:

export default function CardLink({ text, image, href }) {

Javascript相关问答推荐

使用typeof运算符获取对象值类型-接收字符串而不是数组

在JavaScript中,是否有一种方法可以创建自定义thable,在等待某些代码后自动触发它?

如何在不分配整个数组的情况下修改包含数组的行为主体?

RxJS setTimeout操作符等效

为什么从liveWire info js代码传递数组我出现错误?

fs. writeFile()vs fs.writeFile()vs fs.appendFile()

Bootstrap动态选项卡在切换选项卡后保持活动状态,导致元素堆叠

在页面上滚动 timeshift 动垂直滚动条

MathJax可以导入本地HTML文档使用的JS文件吗?

我的角模板订阅后不刷新'

如何在ASP.NET中使用Google Charts API JavaScript将条形图标签显示为绝对值而不是负值

rxjs插入延迟数据

如何将数据块添加到d3力有向图中?

在FAQ Accodion应用程序中使用React useState时出现问题

Next.js无法从外部本地主机获取图像

在单击按钮时生成多个表单时的处理状态

更新文本区域行号

Played link-Initialize.js永远显示加载符号

如何阻止外部脚本进入顶层导航

需要从对象生成列表