gpt4 book ai didi

angular - Angular 6 中模块声明的意外值

转载 作者:太空狗 更新时间:2023-10-29 17:30:11 26 4
gpt4 key购买 nike

In this plunk我有一个 Angular 6 模块,其中包含包装 HttpClient 的相关服务。该模块由应用模块导入,主要应用组件调用服务。

问题是我收到以下错误:

Unexpected value 'MyHttpService' declared by the module 'CoreServiceModule'. Please add a @Pipe/@Directive/@Component annotation

我检查了所有的导入和声明,没有发现问题。哪里出错了?

模块:

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 {}

和服务:

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: '));
}
}

最佳答案

通常如错误所述,services,pipes 应该在您的模块的提供者下,remove from declarations 并将其添加到 providers,

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

]
})

关于angular - Angular 6 中模块声明的意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51462009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com