Javascript Angular2 服务中的意外值。请添加注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43078640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2 Unexpected value in Service. Please add annotation
提问by Alexandr Belov
In my Angular2 app am getting the following error Error: (SystemJS) Unexpected value 'ReleasesService' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation.
在我的 Angular2 应用程序中,出现以下错误错误:(SystemJS) 模块“AppModule”声明的意外值“ReleasesService”。请添加@Pipe/@Directive/@Component 注释。
My AppModule:
我的AppModule:
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 { }
My ReleasesService:
我的发布服务:
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;
}
}
How to fix it? I reinstalled the Quickstarter(the base for my App), and having the same error when try to create the service.
如何解决?我重新安装了Quickstarter(我的应用程序的基础),并在尝试创建服务时遇到了同样的错误。
回答by yurzui
declarationsis only for declarable classes: Components Directives and Pipes
declarations仅适用于可声明的类:组件指令和管道
You can add ReleasesServiceto providersarray
您可以添加ReleasesService到providers数组
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
providers: [ ReleasesService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
See also
也可以看看
回答by Maximilian Friedmann
I had a similar problem, occurring while a project had angular2 as dependency and a project dependency (with the failing component) as well. Seems like angular2 metadata gets attached to the direct angular2 dependency, so the component in the project dependency wasn't declared in the angular2 of the project.
我有一个类似的问题,当一个项目有 angular2 作为依赖项和一个项目依赖项(带有失败的组件)时发生。似乎 angular2 元数据附加到直接的 angular2 依赖项,因此项目依赖项中的组件未在项目的 angular2 中声明。
Workaround is to remove angular2 from the dependency (declaring it as devDependency there) and only use one angular2 instance.
解决方法是从依赖项中删除 angular2(在那里将其声明为 devDependency)并且只使用一个 angular2 实例。
回答by Nirbhay Jha
Be sure the decorator has the caracter @.
确保装饰器具有 @ 字符。
If you don′t type @ before the decorator function you will have this error message
如果你没有在装饰器函数前输入@,你会得到这个错误信息
@Component({ selector: '...', }) -> Correct
Component({ selector: '...', }) -> ERROR MESAGE: 'add a @Pipe/@Directive/@component'

