下一个/图像不使用道具作为图像源
我的home
页面通过postslider
通过props
我的postslider组件将数据发送到我的strapi
cms到我的postslider
import React from "react";
import styles from './index.module.scss'
import { AxiosService } from '../utils/axios-service'
import PostSlider from '../components/postSlider/postSlider'
const Home = ({ posts }) => {
return (
<div id='contentsWrap' className={styles.dohandsWrap}>
<PostSlider home={true} posts={posts} />
</div>
)
}
export default Home
export async function getStaticProps() {
const axios = AxiosService.create()
const res = await axios.get('/archives', {
params: {
category: 'news',
display: true,
showDoson: true,
_limit: 5,
_sort: 'id:DESC'
}
})
return {
props: {
posts: res.data,
},
}
}
。填充我的滑块的数据,
import React from "react";
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import styles from './postSlider.module.scss'
import Link from 'next/link'
import Image from 'next/image'
export default function PostSlider({ home, posts }) {
var settings = {
infinite: posts.length > 2 ? true : false,
autoplay: false,
speed: 500,
autoplaySpeed: 3000,
slidesToShow: 3,
slidesToScroll: 1,
};
return (
<section className={`${styles.postSlider} postSlider ${home ? styles.postSliderHome : 'postSliderNotHome'} ${posts.length > 2 ? 'postSliderPadding' : ''}`}>
<Slider {...settings}>
{posts.map((post) => {
const date = new Date(post.displayDate);
return (
<Link key={post.id} href={`/news/${post.id}`}>
<a className={styles.postSliderLink}>
<article>
<Image src={post.images[0]?.url} alt={post.images[0]?.alternativeText} width={376} height={190} layout="fixed" />
</article>
</a>
</Link>
)
})}
</Slider>
</section>
);
}
我确保在module.exports
in next.config.js
中包含我的cdn地址,但我会收到以下错误
错误:缺少图像“ SRC”属性。确保您通过 “ SRC”在
next/image
组件的道具中。已收到: {“宽度”:376,“高度”:190}
如果我删除Next/image
正常img
标签的组件,一切正常。
我在做什么错?
My Home
page sends data from my strapi
cms to my PostSlider
component via props
import React from "react";
import styles from './index.module.scss'
import { AxiosService } from '../utils/axios-service'
import PostSlider from '../components/postSlider/postSlider'
const Home = ({ posts }) => {
return (
<div id='contentsWrap' className={styles.dohandsWrap}>
<PostSlider home={true} posts={posts} />
</div>
)
}
export default Home
export async function getStaticProps() {
const axios = AxiosService.create()
const res = await axios.get('/archives', {
params: {
category: 'news',
display: true,
showDoson: true,
_limit: 5,
_sort: 'id:DESC'
}
})
return {
props: {
posts: res.data,
},
}
}
My postSlider component then maps over the data to fill my slider
import React from "react";
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import styles from './postSlider.module.scss'
import Link from 'next/link'
import Image from 'next/image'
export default function PostSlider({ home, posts }) {
var settings = {
infinite: posts.length > 2 ? true : false,
autoplay: false,
speed: 500,
autoplaySpeed: 3000,
slidesToShow: 3,
slidesToScroll: 1,
};
return (
<section className={`${styles.postSlider} postSlider ${home ? styles.postSliderHome : 'postSliderNotHome'} ${posts.length > 2 ? 'postSliderPadding' : ''}`}>
<Slider {...settings}>
{posts.map((post) => {
const date = new Date(post.displayDate);
return (
<Link key={post.id} href={`/news/${post.id}`}>
<a className={styles.postSliderLink}>
<article>
<Image src={post.images[0]?.url} alt={post.images[0]?.alternativeText} width={376} height={190} layout="fixed" />
</article>
</a>
</Link>
)
})}
</Slider>
</section>
);
}
I made sure to include my cdn address in module.exports
in next.config.js
but I get the following error
Error: Image is missing required "src" property. Make sure you pass
"src" in props to thenext/image
component. Received:
{"width":376,"height":190}
If I remove the next/image
component for the normal img
tag, everything works fine.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在返回前尝试:
然后在返回中:
Try before the return:
Then inside the return:
有时,
&lt; image/&gt;
标记不像应该这样做,也不接受 src 。尝试在返回之前定义URL,然后在 src 属性中传递URL。返回之前:
然后您可以添加:
Sometimes, the
<Image />
tag doesn't work like it should and doesn't accept the src . Try defining the URL before return and then pass the URL in the src property.Just before return:
And then you can add:
好吧,您的一篇文章似乎有一个空的
图像
数组?图像
需要组件具有src
属性,然后您将不确定的
而不是。您可以检查至少有一张图像,然后将其呈现,这样:
Well, it seems that one of your posts has an empty
images
array?Image
component is required to havesrc
property and you passundefined
instead.You can check if there is at least one image and then render it, like that:
在我的情况下,给图像凝结解决了问题
In my case giving image conditinally solved the problem