Angular组件参数如何定义?darkUnderneath属性绑定报错求解
解决Angular组件属性绑定报错问题
你这里的核心问题是用错了装饰器:@Output是用来让子组件向父组件发射事件的,而你要实现的是父组件向子组件传递属性值,这时候应该用@Input装饰器。
修正后的ModalComponent代码:
import { Component, OnInit, Input } from '@angular/core'; export class ModalComponent implements OnInit { @Input() darkUnderneath: boolean = false; // ... 其他代码 }
另外还要确认两个关键点:
- 确保
ModalComponent已经在对应的Angular模块(比如AppModule或自定义模块)的declarations数组中声明,否则Angular无法识别该组件的输入属性。 - 父组件模板里的
blah变量要确保已在父组件类中定义,或者直接写[darkUnderneath]="true"传递固定布尔值。
内容的提问来源于stack exchange,提问作者Hydroper
相关产品推荐
相关产品推荐

