首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngrx中的父组件和子组件

ngrx中的父组件和子组件
EN

Stack Overflow用户
提问于 2017-03-15 11:45:46
回答 1查看 1.3K关注 0票数 0

我正在尝试用几个转储子组件构建一个父组件。

我不太明白如何设计我的转储子组件。理论上,每个子组件必须使用@Input字段接收所有数据.所以:

代码语言:javascript
复制
@Component({
  selector: '[payment-list]',
  ...
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class PaymentList  {
  @Input() payments$: Observable<ISource>;

我试着使用这个输入字段来列出所有的付款都在付款中。这是我的IStore

代码语言:javascript
复制
export interface IStore {
  user: IUser;
  plans: IPlanRedux;
  sources: ISourceRedux;
  errors: IError;
}

export interface ISource {
    id: string;
    type: string;
    customer: string;
}

export interface ISourceRedux {
    byId: { [key: string]: ISource };
    allIds: Array<string>;
}

父组件使用以下方法生成一个Observable<ISource>

代码语言:javascript
复制
private sources$: Observable<ISource>;

constructor(private store$: Store<IStore>) {
    this.sources$ = this.store$
        .select(fromRoot.getSourceEntities);
}

所以,进入我的parent.template.html

代码语言:javascript
复制
<div payment-list></div>
  1. 转储组件需要使用@Input字段接收必需的所有信息吗?那么,如何才能将可观察到的父组件发送到转储组件?这是正确的吗?
  2. 转储组件可以构建自己的Observables并使用它们吗?
  3. 是否有使用ngrx处理此问题的最佳实践方法?
EN

回答 1

Stack Overflow用户

发布于 2017-03-15 17:49:22

您的子组件不需要流,因为它只需要ISource对象来运行。

代码语言:javascript
复制
@Component({
  selector: '[payment-list]',
  ...
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class PaymentList  {
  @Input() payments: ISource;

private sources$: Observable<ISource>;
constructor(private store$: Store<IStore>) {
    this.sources$ = this.store$
        .select(fromRoot.getSourceEntities);
}

<payment-list payments="sources$ | async"></payment-list>

这种方法使您的子组件变得愚蠢,因为它不需要知道存储。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42808831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档