在另一Angular项目中使用自研Angular组件库时出现多种运行时错误
Angular自研组件库运行时错误排查
我在另一仓库的Angular应用中使用自研的Angular组件库,已解决所有编译时错误,但引入库中已声明并导出的组件时出现不同的运行时错误:
- 使用简单的“外部链接”组件时,报错:
TypeError: getLView() is null - 使用文本输入框包装组件时,报错:
TypeError: tView is null - 使用相对复杂的列表视图组件时,报错:
Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used withEnvironmentInjector#runInContext``
组件库通过本地路径作为依赖安装到消费端项目,package.json配置示例:
"dependencies": { "atoms-lib": "file:../path/to/different/repo/dist/atoms-lib" },
消费端应用的AppModule已导入该库的模块,代码如下:
import { AtomsLibModule } from 'atoms-lib'; @NgModule({ declarations: [ ... ], imports: [ BrowserModule, AppRoutingModule, AtomsLibModule, ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
已完成以下排查:
- 库与消费端应用的Angular版本匹配(均为Angular ^15.2.0)
- 无论是库的部分Ivy编译模式还是完整Ivy编译模式,问题均存在
内容的提问来源于stack exchange,提问作者Niki Herl
相关产品推荐
相关产品推荐

