javascript - MDX 中的 Gatsby 静态图像(gatsby-plugin-image)

标签 javascript typescript gatsby

最近我开始使用 Gatsby,现在我正在尝试使用 MDX,在我的 MDX 文件中,我可以通过 GraphQL 使用 Gatsby Image,但我想使用来自 gatsby-plugin-image 的静态图像,但我遇到了错误像这样:

react_devtools_backend.js:2557 Image not loaded https://images.unsplash.com/photo-1597305877032-0668b3c6413a?w=1300


当我尝试在 .tsx 中实现此图像时,它可以工作,所以我想知道它是否可能。
Gatsby 配置
 "gatsby-remark-images",
    {
      resolve: "gatsby-plugin-mdx",
      options: {
        defaultLayouts: {
          default: require.resolve("./src/components/common/Layout.tsx")
        },
        gatsbyRemarkPlugins: [
          {
            resolve: `gatsby-remark-images`,
            options: {},
          },
        ],
      }
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "images",
        path: `${__dirname}/src/images/`,
      },
      __key: "images",
    },
然后在 test.mdx 中,我尝试像这样使用静态图像:
<StaticImage
    src={'https://images.unsplash.com/photo-1597305877032-0668b3c6413a?w=1300'}
    alt={''}
    width={3840}
    height={1000}
    layout={'constrained'}
/>

最佳答案

您不能使用 gatsby-plugin-image直接在 MDX 文档中。 This post on the Gatsby blog解释了如何通过 Frontmatter 传入图像引用 Prop 来间接使用它。
就个人而言,我已经能够这样做:
此示例仅加载本地镜像,请参阅博客文章了解如何引用远程图像,因为它更复杂。
模板组件

import React from "react";
import { graphql } from "gatsby";
import { MDXRenderer } from "gatsby-plugin-mdx";
import Layout from "../components/layout";

const Game = ({ data }) => {
  const { mdx } = data;
  const { frontmatter, body } = mdx;
  return (
    <Layout title={frontmatter.title}>
      <span className="date">{frontmatter.date}</span>
      <MDXRenderer localImages={frontmatter.embeddedImagesLocal}>
        {body}
      </MDXRenderer>
    </Layout>
  );
};

export const pageQuery = graphql`
  query($slug: String!) {
    mdx(slug: { eq: $slug }) {
      slug
      body
      frontmatter {
        date(formatString: "MMMM DD, YYYY")
        title
        embeddedImagesLocal {
          childImageSharp {
            gatsbyImageData
          }
        }
      }
    }
  }
`;

export default Game;
MDX 文档
---
title: Death Stranding
author: Hideo Kojima
date: 2021-05-06
template: game
embeddedImagesLocal:
  - '../images/20210513035720_1.jpg'
---

import { getImage, GatsbyImage } from 'gatsby-plugin-image';

A great game from Hideo Kojima.

<GatsbyImage alt='Sam in a destroyed mall' image={getImage(props.localImages[0])} />

关于javascript - MDX 中的 Gatsby 静态图像(gatsby-plugin-image),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67227977/

相关文章:

javascript - jquery 克隆并添加事件

javascript - 为什么我的 Angular Directive(指令)(使用编译)在某些情况下会阻止输入?

javascript - 我应该使用 connect 还是 hooks 来进行 React Redux,哪个性能更好?

reactjs - 无法根据生产中运行的 Gatsby 应用程序中的 URL 参数更新 JSX 属性

reactjs - 在 gatsby jsx 中添加 `if` 条件

html - Gatsby 将 lang 属性设置为 html

javascript - "test".reverse() 这可能吗?

javascript - THREE.js PerspectiveCamera focusLength 相差两倍,与 FOV 不一致

javascript - 具有状态管理的 Angular 4 中的顺序(事务性)API 调用

javascript - 在学习 AngularJS 之前,我应该了解 JavaScript 中的哪些内容?