无法从模块中导出服务,既没有声明也没有导入'

时间:2017-05-23 11:27:18

标签: angular angular2-services


我试图从我的一个模块导出服务,但我只得到以下错误:

ERROR Error: Uncaught (in promise): 
Error: Can't export value ConfirmDialogService from SharedModule as it was neither declared nor imported!

我的模块如下:

import { NgModule }                 from "@angular/core";
import { CommonModule }             from "@angular/common";
import { FormsModule }              from "@angular/forms";
import { RouterModule }             from "@angular/router";
import { MaterialModule }           from "@angular/material";

import { ConfirmDialogComponent }       from './confirm-dialog/confirm-dialog.component';
import { ConfirmDialogService }         from './confirm-dialog/confirm-dialog.service';

@NgModule({
    imports: [
        RouterModule,
        CommonModule,
        MaterialModule,
        FormsModule
    ],
    providers: [
        ConfirmDialogService
    ],
    declarations: [
        ConfirmDialogComponent 
    ],
    exports: [
        ConfirmDialogComponent 
        ConfirmDialogService
    ]
})
export class SharedModule {}

文件确实存在并在TS中正确引用,但在运行应用程序时会出现错误。

1 个答案:

答案 0 :(得分:69)

您不需要在exports中列出服务,您只能列出组件,指令和管道。对于服务,providers是相关的,否则TypeScript导入就足够了。