在Angular 9中,要在内存中创建多个实例的共享模块,可以按照以下步骤操作:
// shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [CommonModule],
declarations: [],
exports: []
})
export class SharedModule { }
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { SharedModule } from './shared/shared.module';
@NgModule({
imports: [BrowserModule, SharedModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
// feature.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FeatureComponent } from './feature.component';
import { SharedModule } from '../shared/shared.module';
@NgModule({
imports: [CommonModule, SharedModule],
declarations: [FeatureComponent],
exports: [FeatureComponent]
})
export class FeatureModule { }
// feature.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-feature',
template: 'Feature Component
'
})
export class FeatureComponent { }
这样,每次将FeatureComponent添加到应用程序中,都会创建一个新的实例。这就是在内存中创建多个实例的共享模块的解决方法。