Uncaught Error: Can't resolve all parameters for LoginComponent: (?) in Angular 7 and webpack

Uncaught Error: Can't resolve all parameters for LoginComponent: (?) in Angular 7 and webpack

我在angular 7. 编译时出错。我检查了循环依赖, Injectable() 装饰器。没有未使用的依赖项。我在我的服务中使用了@Injectable()。为了您的信息,我正在使用 mac。我找不到任何解决方案。

我的代码如下:

auth.service.ts

    import { Router } from '@angular/router';
    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs';
    import { HttpErrorResponse } from '@angular/common/http';
    import { HttpService } from '../common/modules/http-with-injector/http.service';
    import { UserData } from '../common/modules/auth/auth.module';
    import { dcrypt } from '../common/_classes/functions';

    @Injectable()
    export class AuthService {
      user: any;
      constructor(
        private router: Router,
        private http: HttpService,
      ) {}
    }

component.ts

import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { AuthService } from '../auth.service';
import * as $ from 'jquery';
import { Router } from '@angular/router';
import { ScriptLoaderService } from 'src/app/common/script-loader.service';
import { AlertService } from 'src/app/common/modules/alert/alert.service';
import { Helpers } from 'src/app/common/helpers';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  model: any = {
    email: '',
    password: ''
  };
  loading = false;
  @ViewChild('hasAlert', { static: true }) alertContainer: ElementRef;

  constructor(
    private _script: ScriptLoaderService,
    private router: Router,
    private alert: AlertService,
    private authService: AuthService,
  ) {
    this.getSettings();
  }
}

请帮我解决问题。

您是否在 NgModule 中添加了 AuthService?

@NgModule({
  declarations: [
  ],
  imports: [
  ],
  providers: [
  AuthService //Your service
  ]
})

如 Richards 所述,您也可以使用以下代码注入服务。

@Injectable({ providedIn: 'root' })

此错误可能导致元数据问题:

TypeScript includes experimental support for emitting certain types of metadata for declarations that have decorators.

要启用此实验性支持,您必须在命令行或 tsconfig.json:

中设置 emitDecoratorMetadata 编译器选项

命令行:

tsc --target ES5 --experimentalDecorators --emitDecoratorMetadata

tsconfig.json:

{
 "compilerOptions": {
    "target": "ES5",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
    }
 }