【问题标题】:Uncaught Error: Can't resolve all parameters for UserService (?)未捕获的错误:无法解析 UserService (?) 的所有参数
【发布时间】:2020-01-11 16:04:49
【问题描述】:

编译我的应用程序后,我在控制台中收到以下错误:

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

我不知道该去哪里找。 @Injectable() 存在于 UserService

import {Injectable} from '@angular/core';
import {Observable} from "rxjs";
import {UserModel} from "../../model/user.model";
import {environment} from "../../../environments/environment";
import {HttpClient} from "@angular/common/http";


@Injectable()
export class UserService {

  constructor(
    private _http: HttpClient
  ) {
  }

  getMe(): Observable<UserModel> {
    return this._http.get<UserModel>(environment.adminApiUrl + '/me');
  }
}

app.module.ts 应该包含所有相关模块:

import {BrowserModule} from '@angular/platform-browser';
import {Injector, NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {AppRouting} from "./app.routing";
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import {LoggerModule, NgxLoggerLevel} from "ngx-logger";
import {environment} from "../environments/environment";
import {AuthGuard} from "./service/guard/auth.guard";
import {UserService} from "./api/service/user.service";
import {CommonModule} from "@angular/common";
import {setAppInjector} from "./app-injector";
import {HttpClientModule} from "@angular/common/http";

const ApiServices = [
  UserService
];

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    CommonModule,
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,

    AppRouting,

    // Logging
    LoggerModule.forRoot({
      serverLoggingUrl: '/',
      level: environment.production ? NgxLoggerLevel.OFF : NgxLoggerLevel.TRACE,
      serverLogLevel: NgxLoggerLevel.OFF
    }),
  ],
  providers: [
    AuthGuard,
    ApiServices
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

我仍然收到此错误。

我错过了什么?


如果我从UserService 的构造函数中删除private _http: HttpClient,则应用程序可以工作。好像Injectable() 注释在我的设置中不起作用。

【问题讨论】:

  • @ChrisY 是的。见 app.module.ts。我知道发生了什么事。甚至rm -rf node_modules 跳舞。
  • UserService在哪个模块中提供?
  • 您可以尝试直接在providers 中添加UserService 吗?另外,您是否尝试过重新启动应用程序?
  • 你能在 stackblitz 上重现这个问题吗?
  • 我认为你应该在providers 部分解构const ApiServices = [ UserService ];。喜欢providers: [...ApiServices]

标签: angular typescript


【解决方案1】:

我在 github 上发现了这个问题:“Angular 8: Can't resolve all parameters for Component: (?)”

注意:我使用的是 Angular 7

建议放

"emitDecoratorMetadata": true,

在tsconfig.spec.json 内。但是,它也只能将其放在 tsconfig.json 中:

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "outDir": "./out-tsc/spec",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

检查我的旧项目,我意识到emitDecoratorMetadata 始终设置为true。我创建了上面运行ng new &lt;project-name&gt; 的项目

$ ng --version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 8.3.21
Node: 10.15.2
OS: linux x64
Angular: 
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.803.21
@angular-devkit/core         8.3.21
@angular-devkit/schematics   8.3.21
@schematics/angular          8.3.21
@schematics/update           0.803.21
rxjs                         6.4.0

【讨论】:

    【解决方案2】:

    请注意,我在从 Angular 8 升级到 Angular 9 时遇到了同样的问题。

    问题在于NGXS 状态。

    从这段代码中我错过了@Injectable 部分。

    @State<ActiveFiltersStateModel>({
      name: 'filterAdapters',
      defaults: {
        filters: {}
      }
    })
    @Injectable()
    export class ActiveFiltersState {
    
      constructor() {
      }
    }
    

    Angular 团队一直在努力确保 Ivy 与 向后兼容之前的渲染引擎(“View 引擎”)尽可能。不幸的是,必须进行一些更改 NGXS 和 Ivy 可以无缝地协同工作。

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 2018-12-18
      • 1970-01-01
      • 2018-09-21
      • 2023-03-16
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多