【angular报错】组件设置standalone后:Can‘t bind to ‘ngModel‘ since it isn‘t a known property of ‘input‘.

背景

在练习路由的组件懒加载的时候,将组件设置为standalone,然后组件对应的html中使用的*ngModel报错:

Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’.

在这里插入图片描述
搜了很多解决方法,都是直接import导入FormsModule这个模块解决的。

题外话

在出现这个问题的同时,*ngFor也报警告了。合理推测都是由设置standalone后导致的问题。

The *ngFor directive was used in the template, but neither the NgFor directive nor the CommonModule was imported. Please make sure that either the NgFor directive or the CommonModule is included in the @Component.imports array of this component.

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

参考:这里

在这里插入图片描述
因此,解决方法为:

在这里插入图片描述
问题解决了。

解决方法

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

在这里插入图片描述

组件/模块 要用到的东西,要再次引入,而全局配置只需在根目录引入一次。

猜你喜欢

转载自blog.csdn.net/karshey/article/details/133769523