TAS*_*TER 8 image reactjs next.js nextjs-image
我正在使用Image
Next.js 中的组件(它是 Next.js 的一个新功能)。我试图提供源网址:
{`${API}/user/photo/${blog.postedBy.username}`}
Run Code Online (Sandbox Code Playgroud)
但它向我展示了这个错误。我也改变了我的next.config.js
作为
module.exports = {
images: {
domains: ['localhost'],
},
}
Run Code Online (Sandbox Code Playgroud)
但没有什么对我有用。如果您对此有所了解,请提供帮助。
Sea*_*ghe 50
const src = `${API}/user/photo/${blog.postedBy.username}`;
<Image loader={() => src} src={src} width={500} height={500}/>
Run Code Online (Sandbox Code Playgroud)
这里loader
是一个为您的图像生成 URL 的函数。它将根域附加到您提供的 src 中,并生成多个 URL 来请求不同大小的图像。这些多个 URL 用于自动 srcset 生成,以便您网站的访问者将获得适合其视口大小的图像。
Saf*_*yas 29
编辑next.config.js:
module.exports = {
reactStrictMode: true,
images: {
domains: ['example.com'],
},
}
Run Code Online (Sandbox Code Playgroud)
TAS*_*TER 20
是的,我终于得到了答案。使加载器函数从图像的目的地加载它。
const myLoader=({src})=>{
return `${API}/user/photo/${blog.postedBy.username}`;
}
Run Code Online (Sandbox Code Playgroud)
使用图像标签的加载器属性使用此加载器函数。
<Image loader={myLoader} src={`${API}/user/photo/${blog.postedBy.username}`} width={500}
height={500}/>
Run Code Online (Sandbox Code Playgroud)
这对我来说非常有用
mac*_*ost 18
这里有一大堆过时的答案建议将密钥设置domains
为next.config.js
. 从 Next 12.3.0 开始,这不再正确;请参阅: https: //nextjs.org/docs/messages/next-image-unconfigured-host
相反,应该使用一个remotePatterns
属性,它比旧的稍微复杂一点domains
:
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'example.com',
port: '',
pathname: '/account123/**',
},
],
},
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6742 次 |
最近记录: |