简体   繁体   English

组件中的Angular 4 HttpClient无法解析所有参数

[英]Angular 4 HttpClient in Component Can't resolve all parameters

I started learning Angular 4 and got to the part with HTTP Client.我开始学习 Angular 4 并开始学习 HTTP 客户端。 Right now I'm trying to make an http call from the component (yes, I know I should transfer it to service, but still)现在我正在尝试从组件进行 http 调用(是的,我知道我应该将它转移到服务,但仍然)

But for some reason, when I try to inject HttpClient into my Component I get the next error:但是出于某种原因,当我尝试将 HttpClient 注入到我的组件中时,我收到了下一个错误:

Uncaught Error: Can't resolve all parameters for PromocodeComponent: (?).未捕获的错误:无法解析 PromocodeComponent 的所有参数:(?)。

Here's the code of my component:这是我的组件的代码:

import { Ticket } from '../../classes/Ticket.class'
import { Component, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'promocode',
  templateUrl: './promocode.template.html',
  styleUrls: ['./promocode.styles.scss']
})
export class PromocodeComponent {
  @Input() ticket: Ticket;
  state: String = "normal";
  promocode: String = "";

   constructor(private http: HttpClient) {}

  promocodeValidate(event): void{
    console.log(this.promocode);
    console.log(event);
    this.http.get('/promocode/asdasda').subscribe(data => {
      console.log(data);
    });
  }
}

And my app.module.ts:还有我的 app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MovieBadgeComponent } from './movie-badge/movie-badge.component';
import { TicketComponent } from './ticket/ticket.component';
import { PromocodeComponent} from './promocode/promocode.component';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';


@NgModule({
  declarations: [
    AppComponent,
    MovieBadgeComponent,
    TicketComponent,
    PromocodeComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

It turned out, for me, that I forgot the @Injectable decorator on my service. 事实证明,对我而言,我忘记了服务上的@Injectable装饰器。 In earlier angular (>=2), i remember the @Injectable as being optional... Guess not anymore? 在早期的角度(> = 2)中,我记得@Injectable是可选的......猜不再了?

Turns out, I missed 事实证明,我错过了

"emitDecoratorMetadata": true “emitDecoratorMetadata”:true

in tsconfig 在tsconfig中

fml... FML ...

may be you need to upgrade your development environment : 您可能需要升级开发环境:

  • node 节点
  • angular cli 角度cli
  • version of angular 角度的版本

and try again 然后再试一次

I got the same error when I ran unit testing (a spec.ts file) with Angular. 当我使用Angular运行单元测试(spec.ts文件)时,我得到了同样的错误。

The following is what I did: 以下是我的所作所为:

     import { HttpClientModule, HttpClient } from '@angular/common/http'; 

     beforeEach(async(() => {
       TestBed.configureTestingModule({
         declarations: [ ],
         imports: [
           HttpClientModule
         ],
         schemas: [NO_ERRORS_SCHEMA],
         providers: [
           HttpClient,
         ]
       })
         .compileComponents();
      }));

and the error was gone. 而错误消失了。

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

相关问题 Angular 2错误:(SystemJS)无法解析组件服务的所有参数:(?) - Angular 2 Error: (SystemJS) Can't resolve all parameters for Component Service: (?) Angular - 无法解析服务的所有参数(?) - Angular - Can't resolve all parameters for service (?) 无法解析 Angular 上 AuthService 的所有参数 - Can't resolve all parameters for AuthService on Angular 如何解决无法解决角度上的所有参数错误 - How to resolve Can't resolve all parameters error on angular Angular 2 Typescript-无法解析TooltipService的所有参数 - Angular 2 Typescript - Can't resolve all parameters for TooltipService 错误:无法解析Angular6中MenuController的所有参数 - ERROR in : Can't resolve all parameters for MenuController in Angular6 Angular 2无法解析[object Object]的所有参数 - Angular 2 Can't resolve all parameters for [object Object] 无法在Angular 2中解析MapsPage :(?,NavController,LaunchNavigator,...)的所有参数 - Can't resolve all parameters for MapsPage:(?,NavController,LaunchNavigator,…) in Angular 2 Angular 2错误:(SystemJS)无法解析成员的所有参数:(?)。(…) - Angular 2 Error: (SystemJS) Can't resolve all parameters for Member: (?).(…) 无法解析 ApplicationModule 的所有参数:(?) - Angular 7 JIT - Can't resolve all parameters for ApplicationModule: (?) - Angular 7 JIT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM