简体   繁体   English

如何解决“未捕获的错误:无法解析AuthService的所有参数:(?)。”

[英]How to fix 'Uncaught Error: Can't resolve all parameters for AuthService: (?).'

this error occur when application is started. 启动应用程序时发生此错误。 The terminal is not showing any error but in the browser console. 终端在浏览器控制台中未显示任何错误。 The last action was i import JwtHelperService from '@auth0/angular-jwt' 最后一个动作是我从'@ auth0 / angular-jwt'导入JwtHelperService

The project is in Nodejs, express 4.16.4 and angular 7.2.0 I have included some code please help me to find the error thank you in advance 该项目在Nodejs中,表示4.16.4和angular 7.2.0,我已经包含了一些代码,请帮助我发现错误,谢谢

auth.service.ts 验证服务

import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import "rxjs/Rx";
import { JwtHelperService } from "@auth0/angular-jwt";
import { map } from "rxjs/operators";
import "core-js/es7/reflect";

import { HttpClient } from "@angular/common/http";

const jwt = new JwtHelperService();
export class AuthService {
  @Injectable()
  private decodedToken;
  constructor(private http: HttpClient) {}
  public register(userData: any): Observable<any> {
    return this.http.post("/api/v1/users/register", userData);
  }
  public login(userData: any): Observable<any> {
    return this.http.post("/api/v1/users/auth", userData).map(token => {
      //debugger;
      return this.saveToken(token);
    });
  }

  private saveToken(token): string {
    //debugger;
    this.decodedToken = jwt.decodeToken(token);
    localStorage.setItem("bwm_auth", token.token);
    localStorage.setItem("bwm_meta", JSON.stringify(this.decodedToken));
    return token;
  }
} 

app.module.ts app.module.ts

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { Routes, RouterModule } from "@angular/router";

import { LoginComponent } from "./login/login.component";
import { RegisterComponent } from "./register/register.component";

import { AuthService } from "./shared/auth.service";

const routes: Routes = [
  { path: "login", component: LoginComponent },
  { path: "register", component: RegisterComponent }
];

@NgModule({
  declarations: [LoginComponent, RegisterComponent],
  imports: [
    RouterModule.forChild(routes),
    FormsModule,
    CommonModule,
    ReactiveFormsModule
  ],
  exports: [],
  providers: [AuthService]
})
export class AuthModule {}

error on browser 浏览器错误

compiler.js:2430 Uncaught Error: Can't resolve all parameters for AuthService: (?).
    at syntaxError (compiler.js:2430)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (compiler.js:18984)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (compiler.js:18877)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getInjectableTypeMetadata (compiler.js:19099)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getProviderMetadata (compiler.js:19108)
    at compiler.js:19046
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getProvidersMetadata (compiler.js:19006)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (compiler.js:18725)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleSummary (compiler.js:18555)

image of error in browser 浏览器中的错误图片

@Injectable() should be placed before the class definition @Injectable()应该放在类定义之前

 @Injectable()
 export class AuthService {

 }

You should first add @Injectable to your AuthService 您应该首先将@Injectable添加到您的AuthService中

But this error indicates that the DI cannot resolve the requested parameter asked by the AuthService (HttpClient). 但是此错误表明DI无法解析AuthService(HttpClient)询问的请求参数。

To resolve this issue, you need to include the HttpClient module in your AppModule as this code snippet shows 要解决此问题,您需要在AppModule中包含HttpClient模块,如以下代码片段所示

import { NgModule }         from '@angular/core';
import { BrowserModule }    from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
  imports: [
    BrowserModule,
    // import HttpClientModule after BrowserModule.
    HttpClientModule,
  ],
  declarations: [
     AppComponent,
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

You can see their full documentation here HttpModule 您可以在这里查看其完整文档HttpModule

暂无
暂无

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

相关问题 未捕获的错误:无法解析AppComponent中的所有参数:(?) - Uncaught Error: Can't resolve all parameters for AppComponent: (?) in angular Nest 无法解析 AuthService (AuthRepository, ?) 的依赖项 - Nest can't resolve dependencies of the AuthService (AuthRepository, ?) 错误:无法解析NoteService的所有参数:(?) - Error: Can't resolve all parameters for NoteService: (?) NestJS 错误:“Nest 无法解析 AuthService 的依赖关系”,即使一切都连接正常 - NestJS Error: “Nest can't resolve dependencies of the AuthService” even everything is wired up fine 运行时错误无法解析UserService的所有参数:(?) - Runtime error can't resolve all parameters for UserService: (?) 无法解析GlobalsService的所有参数:(?) - Can't resolve all parameters for GlobalsService: (?) 无法解析ANGULAR中LoginLogin:([[Object Object],?,?)的所有参数 - Can't resolve all parameters for LoginComponent: ([object Object], ?, ?) in ANGULAR 找不到模块:错误无法解析“child_process”,如何解决? - Module not found: Error can't resolve 'child_process', how to fix? 如何修复“ Uncaught ReferenceError:未定义firebase”错误? - How to fix “Uncaught ReferenceError: firebase is not defined” error? “纱线:警告完整性检查:系统参数不匹配”错误。 它到底是什么意思,我该如何解决? - 'Yarn: warning Integrity check: System parameters don't match' error. What exactly does it mean and how can I fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM