Angular 2 个最佳实践:绑定对象与绑定其属性

Angular 2 best practice: binding an object vs binding its properties

子组件最好是输入对象的属性并更改发射器,还是像这样双向绑定到对象:

示例 1

<parent-component>
  <child-component [exampleInput]="object.val" 
                   (valueChanged)="updateObjectValue($event)">
  </child-component>
</parent-component>

对比示例 2

<parent-component>
  <child-component [(obj)]="object"></child-component>
</parent-component>

其中示例 2 的子组件将处理 updateObjectValue 逻辑。

现在在我们的代码中,像这样的组件目前有 4 个输入和 2 个输出,如果通过将它直接绑定到对象来减少到 1 个 属性 就可以了,那就太棒了

我会将事件发射器选项作为推荐方式。 请参考这个 https://angular.io/guide/component-interaction#parent-listens-for-child-event

<parent-component>
     <child-component [exampleInput]="object.val"
        (valueChanged)="updateObjectValue($event)">
     </child-component>
</parent-component>