将Angular Material添加到自定义库 [英] Add Angular Material to a custom library
问题描述
我想编写自己的库并在其他项目中重用,所以我创建了一个新应用并在其中生成了一个库:
I want to write my own library and reuse it in other projects, so I created a new app and generated a library there:
ng new lib-collection
cd lib-collection
ng g library first-lib --prefix va
如何将Angular Material添加到我的库中?我想使用这样的东西:
How to add Angular Material to my library? I wanted to use something like this:
ng add @angular/material --project=first-lib
但是我收到一个错误:在工作区配置(项目中找不到项目主文件/ first-lib / src)
。
But I got an error: Could not find the project main file inside of the workspace config (projects/first-lib/src)
. What's wrong?
推荐答案
您不需要在此添加它。
您需要将其添加到主应用程序中:
You need to add it to your primary application:
ng add @angular/material
然后将<@ angular / material添加到 peerDependencies
c $ c> projects / first-lib / src / package.json (只需从主<$复制一个包含 @ angular / material
的行c $ c> package.json )。
Then add "@angular/material" to the peerDependencies
of the projects/first-lib/src/package.json
(just copy a line with @angular/material
from your primary package.json
).
您刚刚做的事情:
- 您已将库安装到主要的
package.json
上,现在可以在本地运行代码,因为本地的cli cli将使用主要的package.json
和node_modules
- 在发布后将其设置为库的依赖项并将其安装在另一个地方,它将要求您在此安装
peerDependencies
(作为npm警告)
- You installed the library to the primary
package.json
and can now run your code locally, because locally angular cli would use the primarypackage.json
andnode_modules
- You set it as a library's dependency and after you publish it and install at another place it will ask you to also install the
peerDependencies
there (as npm warning)
您当然也可以将其添加到 dependencies
而不是 peerDependencies
并自动将其安装到您的库中,但这不是管理前端依赖项的好方法(但是可以仍然适合纯node.js软件包)
You can, of course, also add it to dependencies
instead of peerDependencies
and get it automatically installed with your library, but this is not a good way of managing frontend dependencies (but could be still good for pure node.js packages)
这篇关于将Angular Material添加到自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!