我有一个外部模块,我想启用与主应用程序的通信(从应用程序)调用CustomModuleInterfaceService上的一个函数:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import {CustomModuleInterfaceService} from './services/custom.module.interface.service';
import {HttpModule} from "@angular/http";
import { CustomModuleComponent} from './custom.module.component';
@NgModule({
imports: [BrowserModule, HttpModule],
providers: [CustomModuleInterfaceService],
declarations: [CustomModuleComponent],
exports: [CustomModuleComponent]
})
export class CustomModule { }
我在应用中导入了我的CustomModule:
@NgModule({
imports: [
BrowserModule,
routing,
HttpModule,
CustomModule
],
declarations: [
AppComponent
],
providers: [
{ provide: ErrorHandler, useClass: LogService }
],
bootstrap: [AppComponent]
})
export class AppModule { }
在我的AppComponent中,我想在CustomModuleInterfaceService上调用一个函数,但它不起作用:
import {Component} from '@angular/core';
import {CustomModuleInterfaceService} from '...';
@Component({
selector: 'main-app',
template: `<router-outlet></router-outlet>`
})
export class AppComponent {
private interfaceService : CustomModuleInterfaceService;
constructor (_interface:CustomModuleInterfaceService) {
this.interfaceService = _interface;
//MyFunction is defined in CustomModuleInterfaceService
this.interfaceService.MyFunction();
}
}
提前谢谢。
答案 0 :(得分:0)
您的代码似乎适用于我的基本服务。问题可能位于您尝试注入的服务上。你能展示一下这项服务的代码吗?请注意,官方文档中详细说明了“接口”的注入:https://angular.io/docs/ts/latest/guide/dependency-injection.html#dependency-injection-tokens