我具有以下文件夹结构,
Root Module (Apps)
Core Module (Library)
-- ApiService
Shared Module (Library)
-- Ui-Components
Feature Module (Library)
-- ProductCatalog
我已将ApiService导入AppModule / rootModule
app.module.ts
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, CoreModule.forRoot()],
providers: [],
bootstrap: [AppComponent]
})
这是核心模块的实现
import {
NgModule,
Optional,
SkipSelf,
ModuleWithProviders
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { AsyncService } from './async.service';
export function throwIfAlreadyLoaded(parentModule: any, moduleName: string) {
if (parentModule) {
const msg = `${moduleName} has already been loaded. Import Core modules in the AppModule only.`;
throw new Error(msg);
}
}
@NgModule({
imports: [CommonModule]
})
export class CoreModule {
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
throwIfAlreadyLoaded(parentModule, 'CoreModule');
}
static forRoot(): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [AsyncService]
};
}
}
import { Injectable } from '@angular/core';
import { CoreModule } from '@demo9/core';
@Injectable({
providedIn: 'root'
})
export class AsyncService {
constructor() {}
getData() {
const data = [
{
Id: 1,
name: 'data1'
},
{
Id: 2,
name: 'data2'
}
];
return data;
}
}
现在,想知道我们如何在功能特定服务中访问ApiService服务。我有点困惑。请提出建议。