Angular 2无法解析所有参数

时间:2016-10-16 11:52:47

标签: angular typescript

我已经玩了一两个小时试图让我的应用程序运行但我不能再使用简单的服务注入工作了。

我有一个核心模块,其中提供了GlobalsService

core.module.ts

@NgModule({
  declarations: [
    CoreComponent,

    SiteHeaderComponent,

    // Pages
    DocumentationComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot([
      {path: 'documentation', component: DocumentationComponent},
      {path: '', component: CoreComponent}
    ]),
    DocumentationModule,
    SharedModule
  ],
  providers: [
    GlobalsService,
    RegexService,
    UtilsService
  ],
  bootstrap: [CoreComponent]
})
export class CoreModule {}

但是当我尝试在site-header.component.ts中使用它时:

import {Component} from '@angular/core';
import {GlobalsService} from '../../shared';

@Component({
  selector: 'site-header',
  templateUrl: './site-header.component.html',
  styleUrls: ['./site-header.component.scss']
})
export class SiteHeaderComponent {

  ns: string;

  constructor(private globals: GlobalsService) {
    this.ns = globals.ns;
  }
}

globals.service.ts

import {Injectable} from '@angular/core';

@Injectable()

export class GlobalsService {

   // App namespacer
   ns: string = 'test';
}

它告诉我它无法解析GlobalsService的参数,尽管它可以找到该服务,因为如果我从../../shared更改它的导入,则会抛出错误说明无法找到它,所以这不是问题的所在。

修改

我有一个shared文件夹,其中包含指令,管道,服务和共享模块。在其中我使用index.ts文件导出所有内容,因此路径仅为../../shared而不是../../shared/services/globals.service.ts

见图:

shared folder

shared/services/index.ts

export * from './globals/globals.service';
export * from './regex/regex.service';
export * from './utils/utils.service';

shared/index.ts

export * from './directives';
export * from './modules';
export * from './pipes';
export * from './services';

有人可以弄清楚我在这里失踪的是什么吗?

1 个答案:

答案 0 :(得分:1)

我猜你必须从共享文件夹

中输入文件名
import {GlobalsService} from '../../shared/global.service';

而不是

import {GlobalsService} from '../../shared';
相关问题