【发布时间】:2021-09-09 20:44:08
【问题描述】:
我正在使用 react dropzone 在我的简单应用程序中上传多张图片。为了显示要删除哪种类型的图像,我使用 TypeScript 制作了一个单独的组件。但是 Next.js 图像 src 显示错误,例如 Type:
'{ src: string; alt: string; }' is not assignable to type 'IntrinsicAttributes & ImageProps'.
Type '{ src: string; alt: string; }' is not assignable to type 'ObjectImageProps'.
Types of property 'src' are incompatible.
Type 'string' is not assignable to type 'StaticImport'.
渲染文件.ts:
import { IFile } from "../../libs/types";
import { sizeInMb } from "../../libs/sizeInMb";
import { FunctionComponent } from "react";
import Image from "next/image"
const RenderFile: FunctionComponent<{
file: IFile;
}> = ({ file: { formate, sizeInBytes, name } }) => {
return (
<div>
<Image src={`/images/${formate}.png`} alt="image"/>
<span>{name}</span>
<span>{sizeInMb(sizeInBytes)}</span>
</div>
);
};
export default RenderFile;
types.ts:
export interface IFile {
name: string;
sizeInBytes: number;
formate: string | number;
id?: string;
}
我在 src 道具中的错误是什么?
【问题讨论】:
-
你是从本地路径还是远程cdn加载图片?
-
如果你在本地加载图片,那么你应该使用
import image from "img/path";&<Image src={image} alt="something" -
不,兄弟,我正在为此使用 API 调用...如果图像来自本地,那就太容易了。
-
添加
width和height道具将解决它。您可能还想添加layout。 -
如果它来自远程 api 调用那你为什么使用
/images/?在您的远程 api 响应中,它应该是一个带有 url 的 cdn 链接。所以它应该是<Image src={url} />这样的东西。
标签: typescript next.js nextjs-image