【问题标题】:Loading .svg files in img tag with nextjs9使用 nextjs9 在 img 标签中加载 .svg 文件
【发布时间】:2019-11-25 19:49:22
【问题描述】:

正在处理从 Nextjs7 到 Nextjs9 的迁移项目,并且无法在图像标签中包含 .svg 文件。

演示: https://codesandbox.io/s/nextjs-mh9fp?fontsize=14&hidenavigation=1&theme=dark

因为它是一个现有的项目。我想让 .svg 文件与 img 标签本身一起使用。

import React from "react";
import { render } from "react-dom";
import Hello from "./Hello";
import Head from "next/head";
import RawData from "./static/raw_data.txt";
import SVGFile from "./static/SVGFile.svg";

const App = () => (
  <div>
    <Head>
      <title>Trying out next.js</title>
    </Head>
    <div style={{ background: "green" }}>
      <img src={RawData} />
    </div>

    <div style={{ background: "red" }}>
      <h3>SVG IMAGES Included in img tag NOT LOADING</h3>
      <img src={SVGFile} />{/*Here is the problem*/}
    </div>
  </div>
);

【问题讨论】:

标签: javascript reactjs webpack next.js


【解决方案1】:

Next.js 默认不支持图片导入,需要使用file-loader。

// next.config.js
module.exports = {
  webpack(config) {
    config.module.rules.push({
      test: /\.(png|jpe?g|gif|svg)$/i,
      loader: 'file-loader',
      options: {
        outputPath: 'images',
      },
    });
    return config;
  }
};

【讨论】:

  • 我已经做出了您在此处发布的更改:codesandbox.io/s/nextjs-ynkv6。但这似乎没有帮助,如果您有任何线索,请您看看并建议我
  • 代码沙箱未使用 next.js(但常规客户端 React)且未使用从 js 加载的图像。
  • 谢谢哥们,可以吗,如果你能演示一下,导入组件有点代码,即'with import:' 如何使这也起作用?
  • 我已经更新了我的沙箱,codesandbox.io/s/helloworld-c5b8u
  • 我有问题,你能看看stackoverflow.com/questions/59542343/…
猜你喜欢
  • 2019-05-27
  • 2014-11-13
  • 1970-01-01
  • 2021-12-11
  • 2018-08-10
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
相关资源
最近更新 更多