【问题标题】:React does not load local svgReact 不加载本地 svg
【发布时间】:2020-11-24 01:25:40
【问题描述】:

我需要将本地 SVG 调用加载为字符串 './MySvg.svg',但如果我将其作为组件调用,它就可以工作,例如 <MySvg />

我按照这个教程https://blog.logrocket.com/how-to-use-svgs-in-react/,安装了文件加载器,但问题仍然存在。

我正在尝试使用 react-d3-graph 将自定义 svg 放在图形的节点上,但它需要本地 svg 作为字符串并且不接受作为组件。

Svgteste只是导出svgFile.svg的svg标签

一个组件的例子

import * as React from 'react';
import { PageSection, Title } from '@patternfly/react-core';
import { Svgteste } from './Svgteste'
import { Test } from './svgFile.svg'
const Dashboard: React.FunctionComponent = () => {

  return(
    <PageSection>
        <Svgteste /> {/*this works*/}
        <img src={'./svgFile.svg'} /> {/*this won't work*/}
        <img src={Test} /> {/*neither this*/}
    </PageSection>
  )
}

export { Dashboard };

我的网络包

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const BG_IMAGES_DIRNAME = 'bgimages';

module.exports = env => {

  return {
    entry: {
      app: path.resolve(__dirname, 'src', 'index.tsx')
    },
    module: {
      rules: [
        {
          test: /\.(tsx|ts|jsx)?$/,
          use: [
            {
              loader: 'ts-loader',
              options: {
                transpileOnly: true,
                experimentalWatchApi: true,
              }
            }
          ]
        },
        {
          test: /\.(svg|ttf|eot|woff|woff2)$/,
          // only process modules with this loader
          // if they live under a 'fonts' or 'pficon' directory
          use: {
            loader: 'file-loader',
            options: {
              // Limit at 50k. larger files emited into separate files
              limit: 5000,
              outputPath: 'fonts',
              name: '[name].[ext]',
            }
          }
        },
        {
          test: /\.svg$/,
          include: input => input.indexOf('background-filter.svg') > 1,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 5000,
                outputPath: 'svgs',
                name: '[name].[ext]',
              }
            }
          ]
        },
        {
          test: /\.svg$/,
          // only process SVG modules with this loader if they live under a 'bgimages' directory
          // this is primarily useful when applying a CSS background using an SVG
          include: input => input.indexOf(BG_IMAGES_DIRNAME) > -1,
          use: {
            loader: 'svg-url-loader',
            options: {}
          }
        },
        {
          test: /\.svg$/,
          // only process SVG modules with this loader when they don't live under a 'bgimages',
          // 'fonts', or 'pficon' directory, those are handled with other loaders
          
          include: input => (
            (input.indexOf(BG_IMAGES_DIRNAME) === -1) &&
            (input.indexOf('fonts') === -1) &&
            (input.indexOf('background-filter') === -1) &&
            (input.indexOf('pficon') === -1)
          ),
          
          use: {
            loader: 'raw-loader',
            options: {}
          }
        },
        {
          test: /\.(jpg|jpeg|png|gif)$/i,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 5000,
                outputPath: 'images',
                name: '[name].[ext]',
              }
            }
          ]
        },
        {
          test: /\.svg$/,
          use: ['@svgr/webpack'],
        },
        {
        test: /\.(png|jp(e*)g|svg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              //name: 'images/[hash]-[name].[ext]',
            },
          },
        ],
      },

      ]
    },
    output: {
      filename: '[name].bundle.js',
      path: path.resolve(__dirname, 'dist')
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: path.resolve(__dirname, 'src', 'index.html')
      }),
      new Dotenv({
        systemvars: true,
        silent: true
      })
    ],
    resolve: {
      extensions: ['.js', '.ts', '.tsx', '.jsx'],
      plugins: [
        new TsconfigPathsPlugin({
          configFile: path.resolve(__dirname, './tsconfig.json')
        })
      ],
      symlinks: false,
      cacheWithContext: false
    }
  }
};

【问题讨论】:

    标签: reactjs svg patternfly


    【解决方案1】:

    我认为,您必须删除导入中的花括号。花括号表示您要导入以给定名称导出的组件,而导入 svg 时并非如此。

    import Test from './svgFile.svg'
    

    【讨论】:

    • 感谢您的回复。我试过了,还是不行。
    猜你喜欢
    • 2020-04-23
    • 1970-01-01
    • 2023-03-11
    • 2016-04-07
    • 2023-01-03
    • 2015-03-12
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多