English 中文(简体)
2. v-pl草的类型定义?
原标题:Type definition for vite-plugin-svgr?

I have typescript error while adding props to Svg as ReactComponent- type is not assignable to type IntrinsicAttributes . Plugin: vite-plugin-svgr

vite.config.ts -

svgr({
      exportAsDefault: true,
      svgrOptions: {
        icon: true,
      },
    })

增 编

"types" : "vite-plugin-svgr/client" 

to and

import Icon from "./path"

Any suggestions?

问题回答

对我来说,我没有创造任何习俗档案,你只是需要。

  1. In tsconfig.json
"compilerOptions": 
{
    [...rest...]
    "types": ["vite-plugin-svgr/client"] <--- this line
},
  1. In vite.config.ts
import svgr from "vite-plugin-svgr";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), svgr()],
});
  1. In your tsx file in order to use it
import { ReactComponent as MySuperCustomIconComponent } from "../assets/logo.svg";

and simply use it

<MySuperCustomIconComponent/>

有一个声明助手,即你只需要附上<条码>-env.d.ts。 我有与其他解决办法相容的问题。

// vite-env.d.ts
// ...
/// <reference types="vite-plugin-svgr/client" />

Expanding on https://stackoverflow.com/a/75036507/8645558 For anyone thats trying to import svg as react component import { ReactComponent as Settings } from @/assets/navicons/Settings.svg ;

您需要增加出口,然后再增加出口。

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

  export const ReactComponent: React.FunctionComponent<
    React.ComponentProps< svg > & { title?: string }
  >
  export default ReactComponent
}

为在违约时将特殊产品进口作为反应部分,请将<代码>*.svg进口到vite-plugin-svgr的进口列入<代码>vite.config.ts。 档案:

svgr({
  include:  **/*.svg 
})

接着,为了确定打字错误,你必须宣布<代码>*>。 模块类型定义 www.un.org/Depts/DGACM/index_french.htm 档案:

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

  const ReactComponent: React.FunctionComponent<
    React.ComponentProps< svg > & { title?: string }
  >;

  export default ReactComponent;
}

/// <reference types="vite/client" />

That s because you are exporting svg file as default and the typescript inside vite-plugin-svgr/client file doesn t have default declaration.

Let s try this:

  • Create a declaration (.d.ts file)
  • Add this declaration:
declare module  *.svg  {
  import * as React from  react 

  const ReactComponent: React.FunctionComponent<
    React.ComponentProps< svg > & { title?: string }
  >
  export default ReactComponent
}

解决办法一最后是:

  1. Create an svg.d.ts file with this content:

    declare module  *.svg  {
     const content: React.FC<React.SVGProps<SVGElement>>;
     export default content;
    }
    
  2. Add /// <reference types="./{path}/{relative}/{to}/{vite-env.d.ts}/svg" /> to the vite-env.d.ts before any other triple-slash directives. Example:

    /// <reference types="./@types/svg" />
    /// <reference types="vite/client" />
    

P. S.

基本上,这是另一种提供的解决办法:link

declare module  *.svg  {
  const content: React.FC<React.SVGProps<SVGElement>>
  export default content
}

/// <reference types="vite/client" />

这为我提供了建议的解决办法,因为尽管svg got打上了字,但vite/client打字却不复存在。

I had the same error when I changed bundler to Vite. Yarn install was also giving an error and saying Vite needs node 18. So I changed my node version from 16 to 18 with nvm then yarn install and then yarn dev. The problem solved

如果你想要进口假文件,作为反应委员会的责任,那么吗? 反应充其量,然后压倒以下文字:

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

目 录

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

  export const ReactComponent: React.FunctionComponent<
    React.ComponentProps< svg > & { title?: string }
  >
  export default ReactComponent
}




相关问题
How to simplify SVG code?

Is it possible to simplify / clean up svg code by replacing the use-tags with standard svg elements? Maybe an inkscape plugin? Haven t found anything... Background: I m converting some svgs to javafx ...

How to link from SVG?

What I tried is this <a xlink:target="http://ponyoverflow.com"> <text class="text" x="20" y="718" text-anchor="start">Mail Order Ponies</text> </a> and variations with href ...

Ruby Support for SVG

Is there some library or helper to produce SVG graphics with Ruby? I googled a bit and found many, but all seem to be dusty and very incomplete… Does anyone know about some nice, reliable alternative?

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...