【发布时间】: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