You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular库中创建独立组件并在主应用使用时出现'lib-test'元素未识别的问题求助

Angular库中创建独立组件并在主应用使用时出现'lib-test'元素未识别的问题求助

嘿,我最近碰到一个Angular组件库的问题,折腾了好一阵都没搞定,来问问大家有没有遇到过类似情况!

我是这么操作的:

  • 先创建了一个Angular库:ng generate library my-lib
  • 接着在库里生成了一个独立组件:ng generate component .lib/test,这个组件的代码是这样的:
import { Component } from '@angular/core';

@Component({
  selector: 'lib-test',
  standalone: true,
  imports: [],
  templateUrl: './test.component.html',
  styleUrl: './test.component.css'
})
export class TestComponent {

}
  • 然后打包库:先执行ng build,接着切换到dist目录打包成tgz包:cd ../../dist/my-lib && npm pack
  • 回到我的主应用,安装这个本地包:npm install {pointTofolder}/dist/my-lib/my-lib-0.0.1.tgz

结果当我在主应用里想用这个<lib-test>组件的时候,直接报了错:

'lib-test' is not a known element:

  1. If 'lib-test' is an Angular component, then verify that it is part of this module.
  2. If 'lib-test' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

这已经是最基础的组件复用场景了,我到底哪里操作错了或者漏了什么步骤呀?

备注:内容来源于stack exchange,提问作者JKennedy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 06:53:07