简体   繁体   English

Angular 6 UI 在不使用父模板的情况下实现更改检测

[英]Angular 6 UI implementing change detection without using parent template

I need to make a reusable component in angular that will be packaged into a npm module.我需要在 angular 中制作一个可重用的组件,该组件将被打包到 npm 模块中。 Being new to angular I've hit a roadblock with the UI change detection.作为 angular 的新手,我在 UI 更改检测方面遇到了障碍。

for the component to work first the application needs to make an api call to collect the data required for setup then the component needs to run some initalization.为了让组件首先工作,应用程序需要调用 api 来收集设置所需的数据,然后组件需要运行一些初始化。

this.dataService.getData().subscribe((data) => {
   this.data = data;
   this.npmComponent.init(this.data);
});


<app-npm-component></app-npm-component>

In my component I then need to run a bunch of setup tasks and afterwards it needs to listen for changes in the data rerun a sub set of setup tasks and update the ui.在我的组件中,我需要运行一堆设置任务,然后它需要监听数据的变化,重新运行一组设置任务并更新 ui。

@Component({
...,
changeDetection: ChangeDetectionStrategy.OnPush
});

...

init(setupData: any): void {
    this.cdr.detectChanges();
    this.data = setupData;
}

<ul *ngIf="data.arrayOfStuff">
    <li *ngFor="let item of data.arrayOfStuff">{{item}}</li>
</ul>

the usual ChangeDection implementation throws an error: NullInjectorError: No provider for ChangeDetectorRef.通常的 ChangeDection 实现会引发错误:NullInjectorError: No provider for ChangeDetectorRef。 since the data isn't being passed from it's parent via the template.因为数据没有通过模板从它的父级传递。

Whats the right way to implement this kind of thing?实现这种事情的正确方法是什么? Thanks in advance for your time.在此先感谢您的时间。

Edit: I found the answer in ViewChild编辑:我在 ViewChild 中找到了答案

@ViewChild(NpmComponent, {static: false}) npmComponent: NpmComponent;

Put an input on the child给孩子一个输入

<app-npm-component [data]="setupData"></app-npm-component>

and in the child component's class并在子组件的 class

@Input() data;

Angular will take care of listening for changes to inputs. Angular 将负责监听输入的变化。

You can also do it with subscribing using the async pipe.您也可以使用异步 pipe 进行订阅。

In the parent component在父组件中

setupData$ = this.dataService.getData();

and pass the data to the child with the async pipe并使用异步 pipe 将数据传递给孩子

<app-npm-component [data]="setupData$ | async"></app-npm-component>

No subscriptions required.无需订阅。

If you need to run the data through a function use a map如果您需要通过 function 运行数据,请使用 map

setupData$ = this.dataService.getData().pipe(map(data => yourFunction(data)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM