【发布时间】:2017-01-16 18:51:48
【问题描述】:
我正在开发一个以不同模块分隔的大型 Angular 2 应用程序,如下所示:
- 应用程序模块:
- user.module
- supplier.module
- shared.module
- 等
从我的共享模块中,我为应用程序中的所有模态调用了一个 Bootstrap 4 模态组件,我使用 @Input 装饰器通过模态配置对象对其进行自定义。这是我的模态组件 .ts 的样子:
modal.component.ts:
export class ModalComponent {
constructor() { }
public modalMethods = new ModalActionMethods();
@Input() modalConf: ModalModel;
}
然后从模态模板内部,我引用 modalConf 对象,如下所示:
modal.template.html:
<div class="bg-modal-container" [hidden]="!modalConf.isOpen">
最后,当我在需要它的任何组件的视图中使用它时,我将数据传递给模态组件:
whatever.component.html:
<modal [modalConf]="modalOptions"></modal>
modalOptions 引用了在我的whatever.component 的主类中定义的对象。
whatever.component.ts
export class WhatevertComponent {
private modalOptions = {
isOpen: false,
title: "Some Modal Title"
// etc.
}
}
到目前为止一切顺利。
现在,对于我的用户模块,因为我知道我将在多个组件中大量使用我的模态组件,所以我想做一些不同的事情,因为这个模块根据路线为不同的组件提供服务:
users.routing.ts:
const routes: Routes = [
{
path: '', component: UsersComponent,
children: [
{ path: 'creation', component: UserCreationComponent },
{ path: 'management', component: UserManagementComponent }
]
}
];
问题:
我想做的是将我的模态组件插入到我的主用户组件中(将 modalOptions 对象放置在它的主类中,并将模态选择器放在视图中,与上面的 whatever-component 相同) ,但也能够以某种方式从其他子组件中修改模态配置,例如创建、管理等,以便我可以在我的模态中显示相关的子组件数据。 我要避免的是在将要使用/操作它的每个子组件中插入(并为其创建单独的 modalConf 对象)我的模态组件。
最好的方法是什么?我愿意接受建议。
【问题讨论】:
-
我从这个问题的解释是您正在尝试实现跨组件通信,为此您可以使用服务类作为中间体来使用事件发射器传递数据
-
如果我没记错的话,这听起来像是 Rxjs 商店的完美用例。 github.com/ngrx/store
-
你好,拉胡尔·辛格。感谢您的反馈意见。我已经将服务用作我的模态组件的一部分,并定义了一个独特的 getDefaultConf() 方法。您认为使用 currentConf 的两个新的 setter 和 getter 方法扩展此服务并将我的 @Input 装饰器绑定到 getter 会成功吗?
标签: javascript angular typescript