如何在 React 打字稿中读取 xml 文件(toolsbox.xml) [英] How to read xml file(toolsbox.xml) in React typescript

查看:26
本文介绍了如何在 React 打字稿中读取 xml 文件(toolsbox.xml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am unable to import xml file in tsx file, but in jsx file i can able to access that xml file

推荐答案

你需要为 typescript 定义一个模块来评估,否则它会尝试寻找类似 toolbox.xml.ts 的东西.

You need to define a module for typescript to evaluate, otherwise it will try to look for something like toolbox.xml.ts.

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

Something along these lines in a file called XML.d.ts (for this placed in a folder @types in src):

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 文件以及所有已安装的类型).

and add "typeRoots": ["src/@types", "node_modules/@types"] to your tsconfig.json (this makes typescript pick up the new typings file along with all installed types).

这篇关于如何在 React 打字稿中读取 xml 文件(toolsbox.xml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆