4

2 つの兄弟の子ルート間で同じリゾルバーを共有するためのより最適な方法があるかどうかを把握しようとしています。以下は、ルートがリゾルバーとどのように関連しているかの例です。

import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
export const routes = [{
        path: '',
        component: parentComponent,
        canActivate: [AuthGuard],
        resolve: {
            someData: someDataResolver
        },
        children: [
            { path: '', redirectTo: '0', pathMatch: 'full' },
            { path: '0', 
                component: someComponent1,
                resolve: {
                    someData1: someData1Resolver,
                    someData2: someData2Resolver,
                }
            },
            { path: '2', 
                component: someComponent2,
                resolve: {
                    someData2: someData2Resolver
                }
            }
            ... a bunch more children routes/components with resolvers

        ]
    }]

現在、各子ルートのリゾルバー呼び出しを繰り返していますが、これは最適ではないと考えています。共有兄弟の子リゾルバーからデータを共有するより良い方法があるかどうかは誰にも分かりますか? 重複したリゾルバーからのデータを共有サービスに設定することを考えました。これにより、他の子兄弟ルートがサービスからのデータにアクセスします (リゾルバーで別の API 呼び出しを行うのではなく)。他に最適なソリューションはありますか?

4

1 に答える 1

8

コンポーネントのない親ルートを使用してレベルを追加し、リゾルバーをこのレベルまで上げることができます。

import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
export const routes = [{
        path: '',
        component: parentComponent,
        canActivate: [AuthGuard],
        resolve: {
            someData: someDataResolver
        },
        children: [
            { path: '', 
                resolve: {
                    someData2: someData2Resolver
                },

            children: [
                { path: '', redirectTo: '0', pathMatch: 'full' },
                { path: '0', 
                    component: someComponent1,
                    resolve: {
                        someData1: someData1Resolver,
                    }
                },
                { path: '2', 
                    component: someComponent2,
                }
                ... a bunch more children routes/components with resolvers
            ]
        ]
    }]
于 2016-10-29T14:51:56.440 に答える