I'm running into an issue with my simple Angular application that I am having trouble debugging. Whenever I try to inject a service into the constructor I get the an error saying Can't resolve all parameters for LoginService (?)
app.component.ts
import { Component, Injector } from '@angular/core';
import { LoginService } from './login.service';
@Component({
selector: 'app-home',
template: require('./app.component.html')
})
export default class AppComponent {
constructor(private loginService: LoginService) {}
}
login.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class LoginService {
constructor(private http: HttpClient) {
}
login() {
// Do http requests here
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HttpClient } from '@angular/common/http';
// Components
import AppComponent from './app.component';
//Services
import { LoginService } from './login.service';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, HttpClientModule],
providers: [LoginService, HttpClient],
bootstrap: [AppComponent]
})
export default class AppModule {}
Seems to be a problem around the injection of the HttpClient but I cant figure out what exactly.
Any ideas what could be wrong here?
原来是缺少导入!
import 'core-js/es7/reflect';
This can also happen if you've upgraded to Angular 9+ but have disabled Ivy and you're using a library that was compiled with ngc with Ivy turned on. The solution is to upgrade to Ivy or to ask the library owner to produce a non-Ivy build as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.