将 Angular Material 添加到自定义库 [英] Add Angular Material to a custom library

查看:31
本文介绍了将 Angular Material 添加到自定义库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写自己的库并在其他项目中重用它,所以我创建了一个新应用并在那里生成了一个库:

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

但我收到一个错误:在工作区配置 (projects/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 don't need to add it there.

您需要将其添加到您的主应用程序中:

You need to add it to your primary application:

ng add @angular/material

然后将@angular/material"添加到projects/first-lib/src/package.jsonpeerDependencies(只需复制一行带有@angular/material 来自您的主要 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).

你刚刚做了什么:

  1. 您将库安装到主要 package.json 并且现在可以在本地运行您的代码,因为本地 angular cli 将使用主要 package.jsonnode_modules
  2. 您将其设置为库的依赖项,在您发布它并安装到另一个地方后,它会要求您也在那里安装 peerDependencies(作为 npm 警告)
  1. You installed the library to the primary package.json and can now run your code locally, because locally angular cli would use the primary package.json and node_modules
  2. 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屋!

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