Javascript 如何在angular 5中将@Input变量设置为可选变量?

Javascript 如何在angular 5中将@Input变量设置为可选变量?,javascript,angular,angular-decorator,Javascript,Angular,Angular Decorator,我想将@Input参数设置为可选 例如: Child1.html: templateUrl:'common-html.html' 儿童1.TS: currentValue : boolean = true; 案例2: Child2.html: templateUrl:'common-html.html' 儿童2.TS: // Not declared **currentValue** Common-HTML.HTML: <app-master [isLogEnabled]="cur

我想将@Input参数设置为可选

例如:

Child1.html:

templateUrl:'common-html.html'
儿童1.TS:

currentValue : boolean = true;
案例2:

Child2.html:

templateUrl:'common-html.html'
儿童2.TS:

// Not declared **currentValue**
Common-HTML.HTML:

<app-master [isLogEnabled]="currentValue"></app-master>
但上述更改在AOT时会导致错误:

类型Child2.Component上不存在属性“isLogEnabled”

因此,我无法更改以下内容的HTML:

<app-master [isLogEnabled]="currentValue"></app-master>
我会:

App-Master-Component.ts

@Input() isLogEnabled: boolean = false; // can change false to true, null or w.e. depending on what you want as the default
// if consumer passes in a value, it will be overridden
然后在Child2.component.html中

<app-master></app-master>

如果Child2.component.ts文件中不存在currentValue,则无法绑定[isLogEnabled]=currentValue。

您不能提供未声明的属性。 但是你可以用这样的方法:

<app-master [isLogEnabled]="currentValue || false"></app-master>
或者你可以试着用“?”


你做不到?事实上,HTML是作为一个集中式的,所以我不能改变这个问题目前还不清楚。但是回答可选输入属性的问题,您可以尝试在子组件中使用isLogEnabled?:any。添加了更多信息可能不那么干净,但您可以使用可选属性传递对象。HTML是常见的,因此我无法更改HTML
get isLogEnabled() {
    return this.isLogEnabled || false;
}
@Input() isLogEnabled?: boolean;