【问题标题】:Uncaught Error: Can't resolve all parameters for StationComponent: (params...)未捕获的错误:无法解析 StationComponent 的所有参数:(参数...)
【发布时间】:2018-07-07 13:01:43
【问题描述】:

我对 Angular 很陌生,但我正在与一个团队一起做一个项目。解决合并冲突后,应用程序停止在我的浏览器中显示,控制台显示此错误:

Uncaught Error: Can't resolve all parameters for StationComponent: (?, [object Object], [object Object], [object Object], [object Object], [object Object], ?).

从错误来看,它看起来像 StationComponent 的构造函数,并且错误中的两个 (?) 与正在导入的 StationService 和 UserServices 对齐。我不知道为什么会抛出这个错误。这两个服务都有 @Injectable 注释,并且它们的导入路径是正确的(我什至让 VSCode 自动重新导入它们)。有人可以帮我吗?

这是组件的类:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { Station } from '../../models/station';
import { Rail } from '../../models/rail';
import { StationService } from '../../services/station.service';
import { DragulaService } from 'ng2-dragula/components/dragula.provider';
import { UtilsService } from '../../services/utils.service';
import { DialogService, DialogComponent } from 'ng2-bootstrap-modal';
import { AddRailComponent } from '../add-rail/add-rail.component';
import { Subject } from 'rxjs/Subject';
import { ApplicationRef } from '@angular/core';
import { UserService } from '../../services/user.service';


@Component({
  selector: 'app-station',
  templateUrl: './station.component.html',
  styleUrls: ['./station.component.css']
})


export class StationComponent implements OnInit {
public static refreshStation: Subject<boolean> = new Subject();

  station: Station; // = new Station(268, 'Station Name', null, null, [126, 127, 128], null);
  rails: Rail[] = [];

  roleId: number;
  constructor(
    private stationService: StationService,
    private route: ActivatedRoute,
    private dragula: DragulaService,
    private utilsService: UtilsService,
    private dialogService: DialogService,
    private appRef: ApplicationRef,
    private userService: UserService) {
    StationComponent.refreshStation.subscribe(
      res => {
        console.log('Refreshing...');
        this.getStation();
        this.getRails();
      }
    );
  }

  ngOnInit() {
    this.getStation();
    this.getRails();

    this.roleId = this.userService.getUsersRole().id;
    this.dragula.drop.subscribe(
      val => {
        this.station.railIds = this.utilsService.map(this.rails, e => e.railId);
        this.stationService.saveRailOrder(this.station);
      }
    );
  }

  getStation() {
    this.station = this.stationService.selected();
  }

  getRails() {
    if (this.station != null) {
      this.stationService.getRails(this.station)
        .subscribe(
        response => {
          this.rails = response;
        },
        err => this.handleError(err)
        );
    }
  }

  handleError(err) {
    console.log(err);
  }

  showAddRail() {
    const disposable = this.dialogService.addDialog(AddRailComponent, { station: this.station}).subscribe(resp =>
      this.stationService.refresh()
    );
  }

}

【问题讨论】:

  • 你是否将这两个服务添加到模块的providers 列表中?
  • 是的,它们都在这里:提供者:[LoginService, StationService, UserService, ProfileService, LogoutService, RailService, RegisterService, GetStationsService, TileService, TaskService, UtilsService],
  • 看看this question。在one of the answers 中,J MADISON 建议检查所有服务装饰器是否正确拼写为@Injectable(),而不是Injectable() 或@Injectable 或@injectable()。
  • 有一个例子是在 app.module.ts 中更改 UserService 的导入顺序让它被发现,但我无法为 StationService 做同样的事情......跨度>

标签: angular typescript dependency-injection


【解决方案1】:
Uncaught Error: Can't resolve all parameters for StationComponent: (?, [object Object], [object Object], [object Object], [object Object], [object Object], ?).

从错误中“?”意味着服务无法被注入。所以在你的情况下'?位于StationService 和UserService 的地方。所以确保将这两个服务添加到app.module中的providers数组中

【讨论】:

    【解决方案2】:

    我不确定我是否有足够的时间继续下去。我要检查的第一件事是确保所有服务都正确导入为模块或应用程序模块中的提供程序。

    【讨论】:

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