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

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

背景

在练习路由的组件懒加载的时候,将组件设置为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中导入。

参考:这里

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

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

解决方法

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

在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

karshey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值