如何在打字稿中导入markdown(.md)文件 [英] How to import markdown(.md) file in typescript
问题描述
我正在尝试导入打字稿中的自述文件,但出现找不到错误模块"
I am trying to import readme files in typescript but getting "error module not found"
我的TS代码
import * as readme from "./README.md"; // here i am getting error module not found
我也尝试过:types.d.ts
I also tried: typings.d.ts
declare module "*.md" {
const value: any;
export default value;
}
我发现在Typescript 2.0中 https://github.com/Microsoft/TypeScript/wiki/什么的TypeScript新功能#typescript-20 他们引入了模块名称中的通配符",以便我们可以包含任何扩展文件.
I found that in typescript 2.0 https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#typescript-20 they have introduced "Wildcard character in module names" using that we can include any extension file.
我只是按照示例 https://hackernoon.com/import-json-into- typescript-8d465beded79 是用于json文件的,我在markdown文件中遵循的是同样的方法,但是没有成功.
I just followed example https://hackernoon.com/import-json-into-typescript-8d465beded79 which is for json files I followed same for markdown files but no success.
我没有使用webpack和任何加载程序,所以我只想从打字稿中获取它
I am not using webpack and any loader, so I just wanted it only from typescript
推荐答案
对于那些使用带有Typescript的React的人:
For those using React with Typescript:
使用以下代码在根目录中创建globals.d.ts文件:
Create a globals.d.ts file in your root directory with the following code:
declare module "*.md";
然后按如下所示导入它:
then import it like this:
import readme from "../README.md" // substitute this path with your README.md file path
这篇关于如何在打字稿中导入markdown(.md)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!