gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:43:09 26 4
gpt4 key购买 nike

我开始学习 Angular 4 并学习了 HTTP 客户端部分。现在我正在尝试从组件进行 http 调用(是的,我知道我应该将它转移到服务,但仍然如此)

但出于某种原因,当我尝试将 HttpClient 注入(inject)我的组件时,出现了下一个错误:

Uncaught Error: Can't resolve all parameters for PromocodeComponent: (?).

这是我的组件的代码:

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

还有我的 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 {}

最佳答案

事实证明,对我来说,我忘记了服务中的 @Injectable 装饰器。在早期的 Angular (>=2) 中,我记得 @Injectable 是可选的……猜猜是不是?

关于javascript - 组件中的 Angular 4 HttpClient 无法解析所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47013231/

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