【问题标题】:React-responsive-carousel (nextJS/strapi) images are not showing on mobileReact-responsive-carousel (nextJS/strapi) 图像未在移动设备上显示
【发布时间】: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


    【解决方案1】:

    我还使用 Strapi“无头”CMS 作为我的后端和 GraphQL API 从前端检索数据,以下是我目前如何实现我的“Strapi Media”图像组件:

    前端/utils/media.js

    export function getStrapiMedia(url) {
      if (url == null) {
        return null
      }
    
      // Return the full URL if the media is hosted on an external provider
      if (url.startsWith("http") || url.startsWith("//")) {
        return url
      }
    
      // Otherwise prepend the URL path with the Strapi URL
      return `${
        process.env.NEXT_PUBLIC_STRAPI_API_URL || "http://localhost:1337"
      }${url}`
    }
    

    前端/utils/types.js

    import PropTypes from 'prop-types'
    
    export const linkPropTypes = PropTypes.shape({
      id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
      url: PropTypes.string.isRequired,
      text: PropTypes.string.isRequired,
      newTab: PropTypes.bool,
    })
    
    export const mediaPropTypes = PropTypes.shape({
      id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
      alternativeText: PropTypes.string,
      mime: PropTypes.string.isRequired,
      url: PropTypes.string.isRequired,
    })
    
    export const buttonLinkPropTypes = PropTypes.shape({
      theme: PropTypes.string,
      text: PropTypes.string.isRequired,
      newTab: PropTypes.bool,
    })
    

    前端/组件/元素/image.js

    import { getStrapiMedia } from "utils/media"
    import Image from "next/image"
    import PropTypes from "prop-types"
    import { mediaPropTypes } from "utils/types"
    
    const NextImage = ({ media, ...props }) => {
      const { url, alternativeText } = media
    
      const loader = ({ src }) => {
        return getStrapiMedia(src)
      }
    
      // The image has a fixed width and height
      if (props.width && props.height) {
        return (
          <Image loader={loader} src={url} alt={alternativeText || ""} {...props} />
        )
      }
    
      // The image is responsive
      return (
        <Image
          loader={loader}
          layout="responsive"
          width={media.width}
          height={media.height}
          objectFit="contain"
          src={url}
          alt={alternativeText || ""}
        />
      )
    }
    
    Image.propTypes = {
      media: mediaPropTypes.isRequired,
      className: PropTypes.string,
    }
    
    export default NextImage
    

    以下是来自另一个组件上下文的示例用例:

    ...
    import NextImage from "./image"
    ...
              <div className="flex flex-row items-center">
                <Link href="/">
                  <a className="h-8 w-32">
                    <NextImage layout="fill" className="image" media={navbar.logo} />
                  </a>
                </Link>
              </div>
    ...
    

    我希望这会有所帮助 - 如果您可以在此处使用更多的源共享,请告诉我。

    我自己只是在学习 Strapi 框架,选择从 Strapi Next Corporate Starter 引导我的软件工程组合、配置文件和博客站点。您是否也在使用 Strapi 引导项目,或者使用他们的 SDK 进行自定义?

    在处理我自己的自定义 Carousel 时,我还发现,如果您将自定义组件实现为部分组件,则需要将 import 语句和数组成员添加到 Strapi 默认值:

    前端/组件/sections.js

    我的用例示例:

    ...
    import CardCarouselGroup from "@/components/sections/card-carousel-group";
    ...
    // Map Strapi sections to section components
    const sectionComponents = {
      "sections.hero": Hero,
      "sections.large-video": LargeVideo,
      "sections.feature-columns-group": FeatureColumnsGroup,
    ...
      "sections.card-carousel-group": CardCarouselGroup
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-23
      • 2020-10-05
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 2022-06-30
      • 1970-01-01
      相关资源
      最近更新 更多