Ionic6使用组件出现错误:Did you add it to @NgModule.entryComponents

缘由

Ionic6Angular8项目中使用组件时出现错误:Error: No component factory found for LoginComponent. Did you add it to @NgModule.entryComponents? 在我的上一篇文章:移动开发:Ionic框架实现注册与登录功能中,实现软件运行时弹出登录页面,使用的就是组件化(component)。
image

然而在最后运行的时候却报错。上网查了资料,大概就是说我现在代码的写法是ionic老版本的,而我的ionic版本是6.6.0。出现这个问题是因为我没有在要是这个组件的页面所在的模块中引入此组件导致。

解决方法(一)

找到页面所在的模块(xxx.module.ts)代码,进行修改,如下:

就是增加entryComponents:[LoginComponent],用于解决上面的问题。并且在declarations中加入LoginComponent,不然还会出现“Component LoginComponent is not part of any NgModule or the module has not been imported into your module.”错误。

image

解决方法(二)推荐

第一个方法只针对组件使用次数为1次的情况,如果这个组件会被多个页面使用的话,解决方法一会出现新的问题。

找到项目app.module.ts文件的代码,进行修改,如下:

entryComponentsdeclarations中分别添加:LoginComponent;原理和方法一是一样的,只不过添加的文件不一样。

image

猜你喜欢

转载自blog.csdn.net/weixin_42343931/article/details/106249314
Add
今日推荐