简体   繁体   English

将解析器数据传递给子组件:Angular

[英]Pass resolver data to child components : Angular

I have a parent component with route.我有一个带有路由的父组件。 Inside that that component I have 3 child components but they don't have any routes.在该组件内部,我有 3 个子组件,但它们没有任何路由。 I am using their selector to render them.我正在使用他们的选择器来渲染它们。 Now i have created a resolver service for the parent component and calling all api's for both parent child components, And i am passing the data to child components using @Input.现在我已经为父组件创建了一个解析器服务,并为两个父子组件调用了所有 api,并且我正在使用 @Input 将数据传递给子组件。

Is there any better approach to pass resolver data to child components, because i am going to use these child components in different parent components so in that case it will be complicated and repetitive.有没有更好的方法将解析器数据传递给子组件,因为我将在不同的父组件中使用这些子组件,所以在这种情况下它将变得复杂和重复。

Parent Component -父组件 -

<div class="dashboard-outer">
    <app-dashboard-leftbar [leftpanelData]="resolverData"></app-dashboard-leftbar>

    <app-dashboard-topbar [toppanelData]="resolverData"></app-dashboard-topbar>

    <div class="dashboard-content-outer">
        <div class="dashboard-content-area">

        </div>
    </div>
    <app-dashboard-rightbar [rightPanelData]="resolverData"></app-dashboard-rightbar>
</div>

Resolver Service -解析服务 -

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {

    const leftpanel = this._ajaxService.apiCall({}, 'dashboard/leftpanel', 'POST');
    const toppanel = this._ajaxService.apiCall({}, 'dashboard/toppanel', 'POST');
    const rightpanel = this._ajaxService.apiCall({}, 'dashboard/rightpanel', 'POST');


    const dataForDashboard = forkJoin(leftpanel, toppanel, rightpanel)
    return dataForDashboard;
  }

Have the common service to keep the data with set and get method.具有使用 set 和 get 方法保存数据的通用服务。 Inject the common service in your dashboard(Parent) component and other child components.在您的仪表板(父)组件和其他子组件中注入公共服务。

Using setData method you can set the data from dashboard(Parent) component使用 setData 方法,您可以从仪表板(父)组件设置数据

Using getData method you can get the data from any of the child component使用 getData 方法,您可以从任何子组件中获取数据

Please refer this example请参考这个例子

https://stackblitz.com/edit/angular-resolver-simple-example01?file=src%2Fapp%2Fcomponents%2Fhello%2Fhello.component.html https://stackblitz.com/edit/angular-resolver-simple-example01?file=src%2Fapp%2Fcomponents%2Fhello%2Fhello.component.html

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

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