如何在 React typescript中读取 xml 文件(toolsbox.xml)

IT技术 reactjs xml typescript blockly
2021-05-04 07:26:53

我无法在 tsx 文件中导入 xml 文件,但在 jsx 文件中我可以访问该 xml 文件

在此处输入图片说明

在此处输入图片说明

1个回答

您需要为 typescript 定义一个module进行评估,否则它会尝试寻找类似toolbox.xml.ts.

在名为 XML.d.ts 的文件中的这些内容(为此放置在 src 中的文件夹 @types 中):

declare module "*.xml" {
  const doc: any; // Change this to an actual XML type
  export default doc;
}

并添加"typeRoots": ["src/@types", "node_modules/@types"]到您的 tsconfig.json (这使得 typescript 选择新的typings 文件以及所有已安装的类型)。