Angular2 服务中的意外值。请添加注释

新手上路,请多包涵

在我的 Angular2 应用程序中出现以下错误 错误:(SystemJS) 模块“AppModule”声明的意外值“ReleasesService”。请添加 @Pipe/@Directive/@Component 注解。

我的 应用模块

 import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { routing } from './app.routes';
import { HttpModule } from '@angular/http';
import { SearchFilter } from '../app/search-filter.pipe';
import { ReleasesService } from '../app/releases/releases.service';
import { AppComponent }  from './app.component';
import { HomeComponent } from '../app/home/home.component';
import { ReleasesComponent } from '../app/releases/releases.component';
import { DistroComponent } from '../app/distro/distro.component';
import { ContactComponent } from '../app/contact/contact.component';

@NgModule({
  imports:      [ BrowserModule, HttpModule, routing ],
  declarations: [ AppComponent,
                  SearchFilter,
                  HomeComponent,
                  ReleasesComponent,
                  ReleasesService,
                  DistroComponent,
                  ContactComponent  ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

我的 发布服务

 import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { IRelease } from './release';
import 'rxjs/add/operator/map';

@Injectable()
export class ReleasesService {
  getReleases() {
    return IRelease;
  }
}

如何解决?我重新安装了 Quickstarter (我的应用程序的基础),并在尝试创建服务时遇到同样的错误。

原文由 Alexandr Belov 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 271
2 个回答

declarations 仅适用于可声明类: 组件指令和管道

您可以将 ReleasesService 添加到 providers 数组

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent ],
  providers:    [ ReleasesService ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

也可以看看

原文由 yurzui 发布,翻译遵循 CC BY-SA 3.0 许可协议

我有一个类似的问题,发生在一个项目有 angular2 作为依赖项和一个项目依赖项(带有失败的组件)时。似乎 angular2 元数据附加到直接的 angular2 依赖项,因此项目依赖项中的组件未在项目的 angular2 中声明。

解决方法是从依赖项中删除 angular2(在那里将其声明为 devDependency)并仅使用一个 angular2 实例。

原文由 Maximilian Friedmann 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题