Angular 2模块通信

标签 angular typescript module angular2-services

我有一个外部模块,我想启用与主应用程序的通信(从应用程序调用)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(); 
  }
}

提前谢谢你。

最佳答案

您的代码似乎适用于基本服务。问题可能出在您尝试注入(inject)的服务上。你能显示服务的代码吗?请注意,官方文档中详细介绍了“接口(interface)”的注入(inject):https://angular.io/docs/ts/latest/guide/dependency-injection.html#dependency-injection-tokens

关于Angular 2模块通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39507367/

相关文章:

javascript - 我们可以从 typescript 代码更改图像源吗?

python - 无法使用从 python 中同一目录中的另一个文件访问的类

javascript - 强制从同一 URL 重新下载 img

angular - 如何在 Angular 2 中将组件设置回初始状态

angular - Angular 2 内部组件

typescript ,对象的类型为 'unknown'

import - 在 TypeScript 中动态导入模块

module - 是否可以不导出 BuckleScript 中的模块?

angular - Ionic 3 中的指令

Angular :错误 NG8001: 'mat-select' 不是已知元素: