I'm learning Angular and its ecosystem, including UI kits like Material 2 and ng-zorro.
According to their source code, they use the @Input
decorator for each allowable config.
This code is from Material 2:
@Input() mode: 'push' | 'over' | 'side';
@Input() autoFocus: boolean;
And this is from ng-zorro:
@Input() nzWidth: number;
@Input() nzCollapsedWidth: number;
Actual inputs are more than these, thus, I'm wondering why they don't define an interface or even class and name it config
, like this:
@Input() config: MatSidenavConfig;
Ref: What's the correct way to communicate with packages from app component?
Are there any things we should be concerned about?
=====Update=====
I created a test at StackBlitz: https://stackblitz.com/edit/angular-1o67xy
Using a config object or not for @Input
are both functional; not sure if I did it wrong.
答案 0 :(得分:0)
不使用对象进行输入配置的原因可以在有角度的官方文档中找到
https://angular.io/guide/lifecycle-hooks
Angular仅在输入属性的值更改时才调用该挂钩。 Angular不在乎配置本身的属性是否已更改。配置对象引用没有更改,因此从Angular的角度来看,报告没有更改!
要解决此问题, 我们需要实现ngDoCheck(),只要运行更改检测,它就会被调用, 这样一来,我们就可以在其中实施一些逻辑,例如记住旧值并将其与新值进行比较
为避免所有这些麻烦的逻辑,而不是通过对象传递值,我们通过基元传递它们。
希望这会有所帮助。