将自定义类型文件添加到 Angular CLI
我有一个带有 Angular CLI 框架的 SPA 和一个由团队成员编写的 Javascript 库.我需要将此 JS 文件注入到应用程序中.我的想法是
I have a SPA with Angular CLI framework and a Javascript library written by a team member. I need to inject this JS file into the app. My thought is
- 为文件编写类型定义;
- 将 JS 文件及其类型定义 (.d.ts) 导入应用程序
我的问题是如何将自定义类型定义文件添加到我的 Angular CLI 应用程序中?(我知道可以从 .angular-cli.json 文件中的 "scripts": []
添加 JS 文件,对吗?)
My question is how to add the custom type definition file into my Angular CLI app? (I know the JS file can be added from "scripts": []
in .angular-cli.json file, right?)
我看到了一些将外部 js 库添加到 angular-cli 的帖子,但它们似乎都在 npm 上,这显然对我不起作用.
I saw some posts for adding external js library to angular-cli, but they all seem on npm which doesn't work for me apparently.
这里有几个选项:
在新创建的应用程序的
src
目录中有一个文件typings.d.ts
,您可以使用它来引用您团队成员的.d.ts
文件,它将在整个应用程序中公开这些类型.
In the
src
dir for newly created applications there is a filetypings.d.ts
which you can use reference your team member's.d.ts
file which will expose those types throughout your application.
在 tsconfig.json
文件中,您可以将包所在的 node_modules
文件夹的路径添加到 typeRoots
数组这将使这些输入也可用.
In the tsconfig.json
file you can add the path to the node_modules
folder where this package lives to the typeRoots
array which will make those typing available too.