【发布时间】:2021-10-10 02:45:32
【问题描述】:
我正在开发 nextJS/Strapi 在线商店,我想使用 react-responsive-carousel 我按照 [ https://www.npmjs.com/package/react-responsive-carousel ] 上的所有步骤操作。
(我在本地工作)
使用来自 Strapi API(单一类型集合)的动态图像,轮播在桌面上可以正常工作,但是当我尝试从那里访问本地主机时,图像不会显示在我的手机上。来自 Strapi 的所有其他图像都可以正常工作,除了 Carousel 中的图像。
轮播代码:
import { imgToUrl } from "../../utils/urls";
import { Carousel } from "react-responsive-carousel";
const index = ({ data }) => {
const images = data.slider.map((slide) => imgToUrl(slide.image)); //imgToUrl is a function that takes the image URL and concatinate it with HTTP://localhost:1337/
return (
<>
<Carousel
autoPlay={true}
emulateTouch={true}
infiniteLoop={true}
showThumbs={false}
width="100%"
>
{images.map((image) => (
<div className="h-full w-full">
<img className="w-full h-full" src={image} />
{/* <p className="legend">Legend 1</p> */}
</div>
))}
</Carousel>
</>
);
};
export default index;
imgToUrl 代码:
export const API_URL =
process.env.NEXT_PUBLIC_API_URL || "http://localhost:1337";
/**
*
* @param {any} image
*
*/
export const imgToUrl = (image) => {
if (!image) {
return "/Products/3.jpg"; //default image when there is not image uploaded
}
if (image.url.indexOf("/") === 0) {
return `${API_URL}${image.url}`; // Concatinates http://localhost:1337 with the image url
}
return image.url;
};
imToUrl 的输出:
http://localhost:1337/uploads/banner_3_d93516ad90.jpg
我将不胜感激。 谢谢。
【问题讨论】:
标签: reactjs next.js carousel strapi