【问题标题】:Angular 2 module communicationAngular 2 模块通信
【发布时间】:2017-01-23 06:13:00
【问题描述】:

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

提前谢谢你。

【问题讨论】:

  • 当代码实际上不能内联运行时,请不要使用截断功能。请改用[ {} ] 工具栏按钮。

标签: angular typescript module angular2-services


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多