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服务。

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

相关内容

热门资讯

透视苹果版!智星德州插件(透视... 透视苹果版!智星德州插件(透视)微乐家乡破解版(确实真的有辅助工具)-哔哩哔哩1、微乐家乡破解版辅助...
长期以来!wepoker辅助插... 长期以来!wepoker辅助插件功能(透视)游戏黑科技夫追求(一直存在有辅助app)-哔哩哔哩1.游...
透视科技!拱趴大菠萝作必弊方法... 透视科技!拱趴大菠萝作必弊方法(透视)中至赣州黑科技辅助软件(本来有辅助辅助器)-哔哩哔哩1、全新机...
透视智能ai!淘宝买wepok... 透视智能ai!淘宝买wepoker透视有用吗(透视)玩吧辅助脚本(一直是有辅助辅助器)-哔哩哔哩1、...
透视肯定!wepoker辅助器... 透视肯定!wepoker辅助器是真的吗(透视)心悦海南苹果版辅助(原来是真的辅助神器)-哔哩哔哩1、...
一直以来!wepoker钻石怎... 一直以来!wepoker钻石怎么看底牌(透视)丫丫老陕开挂(好像真的是有辅助下载)-哔哩哔哩1、丫丫...
透视实锤!wepoker怎么提... 透视实锤!wepoker怎么提高运气(透视)赣湘互娱挂(都是存在有辅助神器)-哔哩哔哩1、赣湘互娱挂...
透视辅助!newpoker脚本... 透视辅助!newpoker脚本(透视)四川微乐小程序辅助器(都是是真的辅助平台)-哔哩哔哩;一、四川...
为切实保障!哈糖大菠萝攻略(透... 为切实保障!哈糖大菠萝攻略(透视)广东雀神智能插件(本来真的是有辅助安装)-哔哩哔哩所有人都在同一条...
透视好友房!wepoker俱乐... 透视好友房!wepoker俱乐部辅助(透视)广西友乐免费辅助使用视频(切实是有辅助软件)-哔哩哔哩1...