简体   繁体   English

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

[英]Angular2 Unexpected value in Service. Please add annotation

In my Angular2 app am getting the following error Error: (SystemJS) Unexpected value 'ReleasesService' declared by the module 'AppModule'. 在我的Angular2应用程序中收到以下错误错误:(SystemJS)模块'AppModule'声明的意外值'ReleasesService'。 Please add a @Pipe/@Directive/@Component annotation. 请添加@ 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 (我的App的基础),并在尝试创建服务时遇到了同样的错误。

declarations is only for declarable classes: Components Directives and Pipes declarations仅适用于可declarations类: 组件指令和管道

You can add ReleasesService to providers array 您可以将ReleasesService添加到providers数组

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

See also 也可以看看

I had a similar problem, occurring while a project had angular2 as dependency and a project dependency (with the failing component) as well. 我有一个类似的问题,当一个项目有angular2作为依赖项和项目依赖项(与失败的组件)时发生。 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中声明。

Workaround is to remove angular2 from the dependency (declaring it as devDependency there) and only use one angular2 instance. 解决方法是从依赖项中删除angular2(在那里将其声明为devDependency)并仅使用一个angular2实例。

Be sure the decorator has the caracter @. 确保装饰者有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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2上的“请添加@NgModule注释”错误 - “Please add a @NgModule annotation” Error on Angular2 Unexpected value 'ButtonModule 请添加@NgModule 注解 - Unexpected value 'ButtonModule Please add a @NgModule annotation 由模块“AppModule”导入的意外值“FormBuilder”。 请添加@NgModule 注释 - Unexpected value 'FormBuilder' imported by the module 'AppModule'. Please add a @NgModule annotation 意外值''由模块导入''。 请添加@NgModule注释 - Unexpected value ' ' imported by the module ' '. Please add a @NgModule annotation 模块'...'导入的意外值'...'。 请添加@NgModule注释 - Unexpected value '…' imported by the module '…'. Please add a @NgModule annotation 模块“AppModule”导入的意外值“FusionChartsModule”。 请添加@NgModule 注解 - Unexpected value 'FusionChartsModule' imported by the module 'AppModule'. Please add a @NgModule annotation 角度6请添加@NgModule注释 - angular 6 Please add a @NgModule annotation editor.js:1021未捕获的错误:模块'UserModule'导入了意外的值'UserService'。 请添加@NgModule批注 - compiler.js:1021 Uncaught Error: Unexpected value 'UserService' imported by the module 'UserModule'. Please add a @NgModule annotation 模块“CoreModule”声明的意外值“TransportData”。 请添加@Pipe/@Directive/@Component 注解。 在内拉 8 - Unexpected value 'TransportData' declared by the module 'CoreModule'. Please add a @Pipe/@Directive/@Component annotation. in nagular 8 错误:由模块“DynamicTestModule”导入的意外值“DomSanitizer”。 请添加@NgModule 注释 - Error: Unexpected value 'DomSanitizer' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM