Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular NGRX:在不使用ngOnChanges或异步管道的情况下更新子组件的输入属性_Angular_Ngrx - Fatal编程技术网

Angular NGRX:在不使用ngOnChanges或异步管道的情况下更新子组件的输入属性

Angular NGRX:在不使用ngOnChanges或异步管道的情况下更新子组件的输入属性,angular,ngrx,Angular,Ngrx,我知道,通过使用Ngrx和容器/呈现组件体系结构,容器智能组件应该是与存储进行通信的组件,并且基本上通过其公开的输入属性将数据传递给子组件。 作为容器组件的一个补充说明,我们正在使用OnPush更改检测策略 我认为实现这一点的最佳选择是将一个可观察对象传递给模板,并使用异步管道将其展开。 差不多 //in the container's component.ts file this.file$ = this.store.pipe(takeUntil(this.destroy$), select

我知道,通过使用Ngrx和容器/呈现组件体系结构,容器智能组件应该是与存储进行通信的组件,并且基本上通过其公开的输入属性将数据传递给子组件。 作为容器组件的一个补充说明,我们正在使用OnPush更改检测策略

我认为实现这一点的最佳选择是将一个可观察对象传递给模板,并使用异步管道将其展开。 差不多

//in the container's component.ts file 
this.file$ = this.store.pipe(takeUntil(this.destroy$), select(getFiles))

// in the container's html.ts
<child-component [sth] = 'files$ | async'
//在容器的component.ts文件中
this.file$=this.store.pipe(takeUntil(this.destroy$),选择(getFiles))
//在容器的html.ts中

为什么组件中需要
文件
值? 不了解您的用例,我认为这是一种糟糕的做法(但在您的情况下可能是必要的)

我更喜欢使用
async
管道让Angular自己处理订阅,以分配本地值,您可以使用
tap
操作符

this.file$ =  this.store
  .pipe(
    select(getFiles), // notice, we're not using takeUntil because Angular handles the subscription
    tap(files => { 
     this.files = files; // assign the value here
    })
) 
现在您可以像以前一样使用Observable:

<child-component [sth] = 'files$ | async'

太好了,蒂姆,我更喜欢这个。这些文件的价值只是一个例子,很多时候,我需要在多个子组件中使用特定值,也可能在容器方法中的某些情况下。Cheers有时在容器组件中,我需要这些值,以便在从容器分派其他操作时将其作为有效负载传递。我是否应该始终使用有效的选择器来获取状态并将其从容器中移除?我们通常将其异步管道传输到子级,子级将值发送回父级以便能够分派它。在你的特效中访问状态是很好的,但只能作为最后的手段。
<child-component [sth] = 'files$ | async'