简体   繁体   English

我无法引用 Next.js 中的图像

[英]I can't reference an image in Next.js

I have a React component that's being used in Next.js page:我有一个在 Next.js 页面中使用的 React 组件:

/pages/index.js /pages/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Layout from "../src/hoc/Layout/Layout";
import Main from "../src/components/Main/Main";

const Index = () => (
   <Layout>
       <Main />
   </Layout>
);
export default Index

In Main.js I have the following code在 Main.js 中,我有以下代码

import macbookIphone from '../../assets/images/mac-iphone.jpg';

I get the following error我收到以下错误

Module parse failed: Unexpected character '�' (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.模块解析失败:意外字符 '�' (1:0) 您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。 See https://webpack.js.org/concepts#loaders (Source code omitted for this binary file)请参见https://webpack.js.org/concepts#loaders (此二进制文件省略了源代码)

I tried doing the following我尝试执行以下操作

In next-config.jsnext-config.js 中

const withImages = require('next-images')
module.exports = withImages()

I'm still getting the same error.我仍然遇到同样的错误。

What am I doing wrong?我究竟做错了什么?

Please see https://nextjs.org/docs/basic-features/static-file-serving请参阅https://nextjs.org/docs/basic-features/static-file-serving

Next.js can serve static files, like images, under a folder called public in the root directory. Next.js 可以在根目录中名为 public 的文件夹下提供静态文件,如图像。 Files inside public can then be referenced by your code starting from the base URL (/).然后,您的代码可以从基本 URL (/) 开始引用 public 中的文件。

At least in our project, we use require for images instead of import.至少在我们的项目中,我们对图像使用 require 而不是 import。 Our next config looks similar to yours.我们的下一个配置看起来与您的相似。

Try the following and see if it helps:尝试以下操作,看看是否有帮助:

const macbookIphone = require('../../assets/images/mac-iphone.jpg');

You can then use your image as the src like this:然后,您可以像这样使用您的图像作为 src:

<img src={macbookIphone}/>

/public/favicon/mail.png /public/favicon/mail.png

=> "/favicon/mail.png" will work => "/favicon/mail.png" 会工作

Using images in Next.js is a bit different:在 Next.js 中使用图像有点不同:

All your static assets like images must be placed in the public directory.所有静态资产(如图像)都必须放在public目录中。

  • If your code is under src directory, ie <app-name>/src/pages , <app-name>/src/components, ... then your public folder must be outside of the src directory.如果您的代码在src目录下,即<app-name>/src/pages , <app-name>/src/components, ...那么您的公共文件夹必须在 src 目录之外。 Public folder cannot be under src as <app-name>/src/public.公共文件夹不能作为<app-name>/src/public.在 src 下<app-name>/src/public. In this case your public directory must be under <app-name>/public .在这种情况下,您的公共目录必须位于<app-name>/public

  • If your code is not under src directory, ie <app-name>/pages, <app-name>/components, ... then your public directory should be under <app-name>/public如果你的代码不在src目录下,即<app-name>/pages, <app-name>/components, ...那么你的 public 目录应该在<app-name>/public

Once you have that sorted, directly refer to the file in the <Image /> component provided by next/image as:排序后,直接引用next/image提供的<Image />组件中的文件为:

import Image from "next/image"
<Image src="/sample-image.png" width="64" height="64" />

or要么

import Image from "next/image"
import sampleImage from "<file-path>"

<Image src={sampleImage} width="64" height="64" />

provided you have a file under public/sample-image.png如果您在public/sample-image.png下有一个文件

If you have an image URL, directly provide it to the 'src' prop.如果您有图片 URL,请直接将其提供给 'src' 道具。

Find descriptive examples related to layouts at: https://github.com/vercel/next.js/tree/canary/examples/image-component在以下位置找到与布局相关的描述性示例: https : //github.com/vercel/next.js/tree/canary/examples/image-component

References:参考:

  1. https://nextjs.org/docs/basic-features/static-file-serving https://nextjs.org/docs/basic-features/static-file-serving
  2. https://nextjs.org/docs/api-reference/next/image https://nextjs.org/docs/api-reference/next/image
  3. https://nextjs.org/docs/basic-features/image-optimization https://nextjs.org/docs/basic-features/image-optimization
  4. https://nextjs.org/docs/advanced-features/src-directory https://nextjs.org/docs/advanced-features/src-directory

You can import images by using next-images您可以使用next-images导入next-images

Step 1第1步

npm install --save next-images

or要么

yarn add next-images

Step 2第2步

// Create next.config.js
const withImages = require('next-images')
module.exports = withImages()

For Typescript对于打字稿

// Add following line in next-env.d.ts
/// <reference types="next-images" />

Restart your server and now you can import your images like重新启动您的服务器,现在您可以导入您的图像,例如

import img1 from "assets/images/cover_images/blog_1.png"

https://github.com/twopluszero/next-images https://github.com/twopluszero/next-images

From Next.js v11 onwards, you can do what you were doing without any additional config:从 Next.js v11 开始,你可以在没有任何额外配置的情况下做你正在做的事情:

import macbookIphone from '../../assets/images/mac-iphone.jpg';

<Image src={macbookIphone} />

// or

<img src={macbookIphone.src} />

Ref: next/image参考: next/image

For earlier versions if you wish to import images instead of putting them in public directory, then you can configure file-loader or url-loader .对于早期版本,如果您希望导入图像而不是将它们放在公共目录中,那么您可以配置file-loaderurl-loader

it worked for me like this, but putting the file in the public folder:它像这样对我有用,但将文件放在公共文件夹中:

<Image width={150} height={100} src={'/punkieslogo.png'} alt="Picture of the author" /> <图片宽度={150} 高度={100} src={'/punkieslogo.png'} alt="作者图片" />

you cannot access images in Next.js like in React.js unless they are stored in 'public' folder.您不能像在 React.js 中那样访问 Next.js 中的图像,除非它们存储在“公共”文件夹中。

This is pretty straightforward.这很简单。

  1. Add your image to the /public folder.将您的图像添加到/public文件夹。
  2. Now reference that image location in the Image src with its absolute path like below:现在在Image src 中引用该图像位置及其绝对路径,如下所示:
<Image
   src="/profile_banner.jpg"
   alt="cover picture"
   layout="fill"
 />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Next.js 中添加自定义图像占位符? - How can I add custom image placeholders in Next.js? 如何使用 next.js 图像并将其附加到背景? - How can I use next.js image and attach it to the background? 在 Netlify 上部署 next.js 时无法解决此错误。 导入的图像和标签引发错误 - Can't solve this error in next.js Deploying on Netlify. Imported Image and tag throwing the error 为什么我无法访问 Next.js 应用程序“_app.js”中的 window? - Why can't I access the window in Next.js app '_app.js'? 如何过滤Next.js中的道具? 无法从 Next.js 中的 componentDidMount 过滤道具中的数据 - How to filter props in Next.js? Can't filter data in props from componentDidMount in Next.js 如何使用动画更改 Next.Js Image Component src? - How Can I Change Next.Js Image Component src with animation? Next.js / NextAuth:为什么我无法在 API 路由中访问 session - Next.js / NextAuth: Why can't I access session in API route 无法使用 Next.js 将适当的 cookies 发送到 API 服务器 - Can't send appropriate cookies to API server with Next.js Next.js 未找到中间件模块:无法解析“fs” - Next.js middleware Module not found: Can't resolve 'fs' 找不到模块:无法解析“fs”,Next.Js - Module not found: Can't resolve 'fs', Next.Js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM