我希望我错过了一些东西,但似乎在以下上下文中,不可能在
@Input
上设置默认值:
bindToComponentInputs
在路由器模块配置中设置为 true在这种情况下,这些方法都不会设置所需的默认值:
@Input() foo: string = 'bar'; // value is overridden with undefined
@Input( { transform: (val: string) => val ?? 'bar' }) foo: string; // transform method is not called by change detection because parent didn't pass this Input value in
我们陷入困境,因为我们定义了很多输入默认值,但我们想打开
bindToComponentInputs
。但在这样做时,我们必须假设,如果 URL 中没有表示我们的默认值,它们将是未定义的。
请告诉我我错了!
当您使用
@Input
时,请始终期待其值
没问题,使用 setter
query!:string //<--define your variable "query"
@Input('query') set _query(value:string){
//see that always execute this even you don't indicate query
console.log("I'm executing this")
this.query=value||'query by defect'
}