简体   繁体   English

在aux路由之间没有共享Angular2服务实例?

[英]Angular2 Service instance not shared between aux routes?

I have an issue with angular. 我有角度的问题。 I have a feature module whose routing configuration looks like this: 我有一个功能模块,其路由配置如下所示:

const routes: Routes = [
{ 
    path: '', 
    children: [
        { path: '', component: CanvasComponent },
        { path: '', component: ControlsComponent, outlet: 'master' },
        { path: '', component: ToolbarComponent, outlet: 'detail-header' },
        { path: '', component: ConsoleComponent, outlet: 'detail-bottom' },
    ]
}];

The outlets are declared in the app.component.html: 出口在app.component.html中声明:

<!-- Navigation -->
<nav *ngIf="authService.loggedIn" id="navigation">
    <iq-navigation></iq-navigation>
</nav>

<!-- Master View -->
<div *ngIf="authService.loggedIn" id="master" [iqResizable]="['right']" [minSize]="250">
    <router-outlet name="master"></router-outlet>
</div>

<!-- Detail View -->
<div id="detail">

    <!-- Detail Header -->
    <div id="detail-header">
            <router-outlet name="detail-header"></router-outlet>  
    </div>

    <!-- Detail Main -->
    <div id="detail-main">
        <router-outlet></router-outlet>  
    </div>

    <!-- Detail Bottom -->
    <div *ngIf="authService.loggedIn" id="detail-bottom" [iqResizable]="['top']">
        <router-outlet name="detail-bottom"></router-outlet>
    </div>

</div>

The route for the feature module is configured in app-routing.module like this: 功能模块的路由在app-routing.module中配置,如下所示:

const routes: Routes = [
{ 
    path: '', 
    redirectTo: '/flow', 
    pathMatch: 'full' 
},
{ 
    path: 'flow', 
    canActivate: [AuthGuard], 
    loadChildren: 'app/flow/flow.module#FlowModule' 
},
{ 
    path: 'data', 
    canActivate: [AuthGuard], 
    loadChildren: 'app/data-manager/data-manager.module#DataManagerModule' },
{ 
    path: 'login', 
    loadChildren: 'app/login/login.module#LoginModule' 
},];

Now both the Canvas Component and the Controls Component are injecting a service, but each one gets its own instance. 现在, Canvas组件控件组件都注入了一个服务,但每个服务都有自己的实例。

I thought that each module (especially since this feature module gets lazily loaded) has its own root injector, which in turn gets appended as a child to the root injector. 我认为每个模块(特别是因为这个特征模块被延迟加载)都有自己的根注入器,而后者又作为子注入到根注入器。 But it seems like each root component of each outlet gets its own root injector (and each one provides my EditorService) and so do not share a common ancestor and thus each get their own service instance. 但似乎每个插座的每个根组件都有自己的根注入器(并且每个都提供我的EditorService),因此不共享共同的祖先,因此每个插件都获得自己的服务实例。

Is this intentional? 这是故意的吗? In my case this screws up my whole architecture. 在我的情况下,这搞砸了我的整个架构。

Thanks for your advice. 谢谢你的建议。

EDIT: Here is the definition of the lazily loaded module (flow.module) 编辑:这是延迟加载模块的定义(flow.module)

@NgModule({
imports: [ ReactiveFormsModule, SharedModule, FlowRoutingModule ],
exports: [], 
declarations: [
    CanvasComponent, 
    ControlsComponent, 
    ...
],
providers: [ 
    EditorService, 
    ....
]

}) })

The Service which gets instantiated twice is the EditorService 实例化两次的服务是EditorService

For the app module there is nothing fancy, but for the sake of completeness: 对于app模块,没有任何花哨的东西,但为了完整起见:

@NgModule({
    imports: [ BrowserModule, AppRoutingModule, CoreModule, SharedModule, LoginModule ],
    exports: [],
    declarations: [ AppComponent ],
    providers: [],
    bootstrap: [ AppComponent ]
})
export class AppModule { }

My Solution Well, i implemented a FlowModule.forRoot() function as suggested by Günter which returns a ModuleWithProvider configured to provide my EditorService and it works. 我的解决方案好吧,我按照Günter的建议实现了一个FlowModule.forRoot()函数,它返回一个EditorService ,它ModuleWithProvider配置为提供我的EditorService并且它可以工作。 I`m still curious though if this behavior is intended, since optimally i would just load my feature module without needing any further configuration steps. 如果这个行为是有意的话我仍然很好奇,因为我最好只加载我的功能模块而不需要任何进一步的配置步骤。 Also i will have to do this for every other feature module and service which shall be shared by different outlets (eg components in the master and detail outlet due to my chosen architecture above) 此外,我将不得不为每个其他功能模块和服务执行此操作,这些功能模块和服务应由不同的插座共享(例如,由于我选择的架构,因此主设备和详细信息插座中的组件)

I thought that each module (especially since this feature module gets lazily loaded) has its own root injector, which in turn gets appended as a child to the root injector. 我认为每个模块(特别是因为这个特征模块被延迟加载)都有自己的根注入器,而后者又作为子注入到根注入器。 But it seems like each root component of each outlet gets attached to the app component and so do not share a common ancestor and thus each get their own service instance. 但似乎每个插座的每个根组件都附加到应用程序组件,因此不共享共同的祖先,因此每个插件都获得自己的服务实例。

Hard to understand for me what you mean by this. 我很难理解你的意思。

If you provide a service in a lazy loaded module, components of this module will get a different instance than lazy loaded components from other modules or non-lazy loaded modules. 如果在延迟加载的模块中提供服务,则此模块的组件将获得与来自其他模块或非延迟加载模块的延迟加载组件不同的实例。

Actually components in non-lazy loaded modules will only get any instance injected at all if there is a provider registered at the appliation root scope and in other lazy loaded modules they also only get an instance if this lazy loaded module also has a provider or the application root scope has a provider. 实际上,非延迟加载模块中的组件只会在应用程序根范围内注册提供程序时才会注入任何实例,而在其他延迟加载模块中,如果此延迟加载模块也有提供程序或者应用程序根范围有一个提供者。

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

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