English 中文(简体)
Unable to import SVG with Vite as ReactComponent
原标题:Unable to import SVG with Vite as ReactComponent

Tried to use this library: vite-plugin-react-svg

and had no success by importing it like:

import { ExternalLink } from  assets/svg/link-external.svg?component ;

Are there any workarounds for this issue?

此前的错误如下:

import { ReactComponent as ExternalLink } from  assets/svg/link-external.svg ;

//Uncaught SyntaxError: 
  The requested module  /src/assets/svg/link-external.svg?import 
  does not provide an export named  ReactComponent 
最佳回答

Anyone having issues with the accepted answer, try the solution described in this package: https://www.npmjs.com/package/vite-plugin-svgr

<代码>import{ReactComponent aslogo } from /logo.svg

出于某种原因,我无法利用已接受的回答。

问题回答

You should use @svgr/rollup instead of vite-plugin-svgr. vite-plugin-svgr is not a good choice, because it doesn t support svgo, and it use esbuild to transform some svg code, it may have some problem in es5 environment.

最糟糕的是vite-plugin-svgr,可添加svgo plugin, see this issue:

npm install @svgr/rollup -D
import { defineConfig } from  vite ;
import react from  @vitejs/plugin-react ;
import svgr from  @svgr/rollup ;

export default defineConfig({
  plugins: [react(), svgr()]
});

<代码>yarn 后加——D vite-plugin-svgr将是最佳选择,因为它保持不变。

// vite.config.js
import svgr from  vite-plugin-svgr 

export default {
  plugins: [svgr()],
}
import { ReactComponent as Logo } from  ./logo.svg 

也可以通过在<条码>中设置这一组别选择而作为违约进口。

svgr({
  exportAsDefault: true
})
// now you can import as default
import Logo from  ./logo.svg 

但是,它注意到,它将凌驾于作为违约而输出危民盟的伏特人的违约行为之上。


如果你有任何类型的文字问题,则还有一份声明帮助者,以更好地进行类型的推论。

/// <reference types="vite-plugin-svgr/client" />

使用<条码>@svgx/vite-plugin-react

安装

npm i @svgx/vite-plugin-react

并将其列入您的档案。

import { defineConfig } from "vite";
import svgx from "@svgx/vite-plugin-react";

export default defineConfig(async () => {
  return {
    plugins: [
      svgx()
    ],
  };
});

之后作为反应部分进口

import MyIcon from "./icon.svg?component";

您可以进口成“公司”或“母”或“批量”。

这一缩略语的最好部分是,你可以动态地进口 files鱼。 比如:如果你想用一个变数的名字进口一只雕像,那么你就可以这样做:

import importVars from "@svgx-dir:/path/to/directory";

export default function (props) {
  const MyIcon = importVars(props.iconName);
  return (
    <div>
      <MyIcon color="#FF5733" />
    </div>
  );
}

现在,你可以通过一个变量进口 甲壳体能动态进口。

关于慢性反应申请,我不得不:

  1. www.un.org/french/ga/president

  2. Add a custom.d.ts file to the project with the following content:

    declare module  *.svg?react  {
        import React = require( react );
        export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
        const src: string;
        export default src;
    }
    

    在<代码>include上添加:tsconfig.json :

    "include": ["src", "custom.d.ts"],`
    
  3. 采用特殊群体:

    import MyIcon from "/src/components/icons/MyIcon.svg?react";
    
    <MyIcon/>
    

你们可以通过进口这种寄给一种 im子而避免gr。

import logo from "./logo-login.svg"
<img src={logo} className="w-24 inline-block" alt="logo" />

我在安装了这套材料后能够解决这个问题:

www.un.org/spanish/ga i vite-plugin-svgr

// vite.config.js
import { defineConfig } from  vite ;
import react from  @vitejs/plugin-react ;
import svgr from  vite-plugin-svgr ; // make sure to import it

export default defineConfig({
    plugins: [react(), svgr()],
});

// App.jsx
import {ReactComponent as ExternalLink} from  ./assets/svg/link-external.svg ;

<ExternalLink />
  1. install pkg npm i vite-plugin-svgr
  2. import with react import AnyName from ../../assets/svgs/icon.svg?react ;
  3. use in jsx <AnyName />

If you want to import svg files as ReactComponent without ?react suffix then override vite.config.ts file as follows:

export default defineConfig({
  // ...
  plugins: [
    // ...
    svgr({
      svgrOptions: { exportType:  named , ref: true, svgo: false, titleProp: true },
      include:  **/*.svg ,
    }),
    // ...
  ],
  // ...
});




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签