简体   繁体   English

为什么 IDE 在我导入 SVG 文件以用作 React 组件时显示错误?

[英]Why IDE shows me error when I import SVG file to use as React component?

I am try to using SVG Icon as React component with svgr我正在尝试使用 SVG 图标作为带有svgr的 React 组件

everything works well.一切正常。 I can imoport SVG and svgr convert it to ReactComponents perfectly.我可以将 SVG 和 svgr 完美地转换为 ReactComponents。

but, My IDE webstorm still shows me error when I import svg like below但是,当我像下面这样导入 svg 时,我的 IDE webstorm 仍然显示错误

在此处输入图像描述

I am using typescript and nextjs as well.我也在使用 typescript 和 nextjs。

what is real problem?什么是真正的问题? is it only caused by IDE?是否仅由 IDE 引起? or Should I setup additional options?或者我应该设置其他选项吗?

here is my config files这是我的配置文件

tsconfig.json tsconfig.json

  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": "src/"
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.svg"
  ]
}

next.config.js next.config.js

module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    // Note: we provide webpack above so you should not `require` it
    // Perform customizations to webpack config
    // Important: return the modified config
    config.plugins.push(new webpack.IgnorePlugin(/\/__tests__\//))

    config.module.rules.push({
      test: /\.svg$/,
      use: [{
        loader: '@svgr/webpack',
        options: {
          svgoConfig: {
            plugins: {
              removeViewBox: false
            }
          }
        }
      }],
    })
    return config
  },
  webpackDevMiddleware: (config) => {
    // Perform customizations to webpack dev middleware config
    // Important: return the modified config
    return config
  },
}

and no babelrc because I am using default babel setting given by nextjs.并且没有 babelrc,因为我使用的是 nextjs 给出的默认 babel 设置。

Thanks in advance提前致谢

I find by my self.我自己找的。 It is caused by typescript.它是由 typescript 引起的。 i had to declare type for .svg我必须为.svg声明类型

I made typing.d.ts file on the root我在根目录下创建了 typing.d.ts 文件

declare module '*.svg' {
  import * as React from 'react';

  export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;

  const src: string;
  export default src;
}

The IDE does not show error any more IDE 不再显示错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将 SVG 文件导入为具有动态 import() 表达式的 React 组件吗? - Can i import the SVG file as a react component with dynamic import() Expressions? 为什么当我使用 avatar antd 时反应会报错? - Why react get me error when I use avatar antd? 使用 SVG 文件作为 React 组件 - Use SVG file as React component React/Typescript 无法将 svg 文件作为组件导入 - React/Typescript cannot import svg file as component 为什么当我将 SVG 导入 React 时,所有组元素都被删除了? - Why when I import an SVG into React, are all the group elements removed? 导入 SVG 作为 react 组件 - Import SVG as react Component 获取模块解析失败:当我尝试为 React 中的特定组件导入 css 文件时出现意外令牌 (1:0) 错误 - Getting Module parse failed: Unexpected token (1:0) error when I try to import css file for a specific component in react 为什么我不能从文件中导入 React 组件? - Why can't I import a React component from a file? 当我尝试在 React Native 中导入我的组件时出错 - have an error when i try import my component in react native 当我在输入文本上使用 fireEvent 时,为什么 React 测试库会给我一个错误? - Why React Testing Library is giving me an error when I use fireEvent on a input text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM