未捕获的错误:无法解析StationComponent的所有参数:(参数...)

时间:2018-01-28 20:33:28

标签: angular typescript dependency-injection

我是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()
    );
  }

}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

从错误中&#39;?&#39;意味着服务无法注入。所以在你的情况下&#39;?&#39;位于StationServiceUserService的位置。因此,请确保在providers

中的app.module数组中添加这两项服务