Angular 6中模块声明的意外值

ps0*_*604 5 angular

在这个插件中,我有一个Angular 6模块,它包含一个相关的服务HttpClient.模块由应用程序模块导入,主应用程序组件调用该服务.

问题是我收到以下错误:

模块'CoreServiceModule'声明的意外值'MyHttpService'.请添加@ Pipe/@ Directive/@ Component注释

我检查了所有的导入和声明,但找不到问题.哪里出错?

模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { MyHttpService } from './http.service';

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule
  ],
  declarations: [
    MyHttpService
  ],
  exports: [
    MyHttpService
  ]
})
export class CoreServiceModule {}
Run Code Online (Sandbox Code Playgroud)

和服务:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

@Injectable({
  providedIn: 'root'
})
export class MyHttpService {

  constructor(public httpx: HttpClient) {
        console.log("In the HttpClient constructor - " + this.httpx) 
  }

  call () {
        let data = { x: 1 };
        this.httpx.post('response.json', data).subscribe(result => {
            console.log("Http result = " + result);
          }, error => console.log('There was an error: '));
    }
}
Run Code Online (Sandbox Code Playgroud)

Saj*_*ran 13

通常,如错误所示,services,pipes应该在您的模块的提供者之下,remove从中declarations添加providers,

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule
  ]
  declarations : [],
  providers:[ 
     MyHttpService
  ],
  exports: [

  ]
})
Run Code Online (Sandbox Code Playgroud)