Angular - 延迟加载2个模块并需要共享相同的服务
创始人
2024-10-15 01:00:45
0

要实现延迟加载2个模块并需要共享相同的服务,可以使用Angular的共享模块和惰性加载模块的概念。

首先,创建一个共享模块,其中包含需要共享的服务和其他共享的组件、指令等。例如,创建一个名为SharedModule的共享模块:

shared.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SharedService } from './shared.service';

@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ],
  providers: [
    SharedService
  ]
})
export class SharedModule { }

然后,创建2个惰性加载的模块,每个模块都需要共享相同的服务。例如,创建一个名为Module1Module的模块:

module1.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SharedModule } from '../shared/shared.module';
import { Module1Component } from './module1.component';

@NgModule({
  declarations: [Module1Component],
  imports: [
    CommonModule,
    SharedModule
  ]
})
export class Module1Module { }

module1.component.ts:

import { Component, OnInit } from '@angular/core';
import { SharedService } from '../shared/shared.service';

@Component({
  selector: 'app-module1',
  template: `
    

Module 1

` }) export class Module1Component implements OnInit { constructor(private sharedService: SharedService) { } ngOnInit() { } callSharedService() { this.sharedService.sharedMethod(); } }

同样地,创建第二个惰性加载的模块Module2Module:

module2.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SharedModule } from '../shared/shared.module';
import { Module2Component } from './module2.component';

@NgModule({
  declarations: [Module2Component],
  imports: [
    CommonModule,
    SharedModule
  ]
})
export class Module2Module { }

module2.component.ts:

import { Component, OnInit } from '@angular/core';
import { SharedService } from '../shared/shared.service';

@Component({
  selector: 'app-module2',
  template: `
    

Module 2

` }) export class Module2Component implements OnInit { constructor(private sharedService: SharedService) { } ngOnInit() { } callSharedService() { this.sharedService.sharedMethod(); } }

最后,在主模块中进行惰性加载。例如,创建一个名为AppRoutingModule的路由模块:

app-routing.module.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: 'module1', loadChildren: () => import('./module1/module1.module').then(m => m.Module1Module) },
  { path: 'module2', loadChildren: () => import('./module2/module2.module').then(m => m.Module2Module) }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

最后,在主模块的AppModule中导入共享模块和路由模块:

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { SharedModule } from './shared/shared.module';
import { AppComponent } from './app.component';

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

现在,当惰性加载Module1Module或Module2Module时,它们都会共享相同的SharedService服务。

请注意,以上示例仅演示了如何共享服务,你可以根据需要在共享模块中添加其他共享的组件、指令等。

相关内容

热门资讯

玩家必看教程!xpoker透视... 玩家必看教程!xpoker透视辅助,wepoker软件安装包,曝光教程(有挂技巧);最新版2024是...
透视科技!aapoker怎么设... 透视科技!aapoker怎么设置提高好牌几率(透视)都是存在有挂(详细辅助力荐教程)运辅助工具,进入...
免费测试版!hhpoker有没... 免费测试版!hhpoker有没有辅助,hhpoker德州透视挂,可靠技巧(有挂辅助);科技安装教程;...
透视玄学!wepoker透视辅... 透视玄学!wepoker透视辅助下载(透视)竟然是真的有挂(详细辅助科技教程)1、游戏颠覆性的策略玩...
安装程序教程!hhpoker必... 1、安装程序教程!hhpoker必备开挂,wepoker有透视吗,2025新版总结(有挂软件);详细...
透视好友房!wpk俱乐部辅助器... 透视好友房!wpk俱乐部辅助器(透视)果然是有挂(详细辅助黑科技教程);1)辅助挂:进一步探索辅助透...
今日焦点!wejoker辅助软... 今日焦点!wejoker辅助软件视频,hhpoker开挂教程,攻略方法(有挂教程)1、玩家可以在we...
透视透视!wepoker私人局... 透视透视!wepoker私人局俱乐部(透视)竟然是有挂(详细辅助教你教程);1、许多玩家不知道辅助软...
透视规律!来玩app 德州 辅... 透视规律!来玩app 德州 辅助(透视)果然有挂(详细辅助黑科技教程);1、每一步都需要思考,不同水...
玩家必看科普!拱趴大菠萝机器人... 玩家必看科普!拱趴大菠萝机器人,aapoker辅助器怎么用,玩家教程(有挂技巧);大神普及一款德州a...