背景
在练习路由的组件懒加载的时候,将组件设置为standalone,然后组件对应的html中使用的*ngModel报错:
Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’.

搜了很多解决方法,都是直接import导入FormsModule这个模块解决的。
题外话
在出现这个问题的同时,*ngFor也报警告了。合理推测都是由设置standalone后导致的问题。
The
*ngFordirective was used in the template, but neither theNgFordirective nor theCommonModulewas imported. Please make sure that either theNgFordirective or theCommonModuleis included in the@Component.importsarray of this component.

由报错内容可知,需要引入CommonModule这个模块。
由于组件已经被设置为standalone,而它本身并没有ngFor这种指令,需要从CommonModule中导入。
参考:这里

因此,解决方法为:

问题解决了。
解决方法
以此类推,解决方法为:在组件中引入对应指令的模块。

组件/模块 要用到的东西,要再次引入,而全局配置只需在根目录引入一次。
博客围绕Angular练习路由组件懒加载时遇到的问题展开。设置组件后,html中使用ngModel报错,同时伴有警告。经分析,因组件设为standalone且本身无该指令,需导入对应模块。解决方法是在组件中引入对应指令的模块,全局配置在根目录引入一次。

被折叠的 条评论
为什么被折叠?



