Angular 未捕获错误:Can';t解析StationComponent的所有参数:(参数…)
我对Angular很陌生,但我正在和一个团队一起做一个项目。解决合并冲突后,应用程序停止在“我的浏览器”中显示,控制台显示此错误:Angular 未捕获错误:Can';t解析StationComponent的所有参数:(参数…),angular,typescript,dependency-injection,Angular,Typescript,Dependency Injection,我对Angular很陌生,但我正在和一个团队一起做一个项目。解决合并冲突后,应用程序停止在“我的浏览器”中显示,控制台显示此错误: Uncaught Error: Can't resolve all parameters for StationComponent: (?, [object Object], [object Object], [object Object], [object Object], [object Object], ?). 从错误中,它看起来像StationCompon
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()
);
}
}
从'@angular/core'导入{Component,OnInit};
从'@angular/router'导入{ActivatedRoute};
从“../../models/Station”导入{Station};
从“../../models/Rail”导入{Rail};
从“../../services/station.service”导入{StationService};
从“ng2-dragula/components/dragula.provider”导入{DragulaService};
从“../../services/utils.service”导入{UtilsService};
从“ng2引导模式”导入{DialogService,DialogComponent};
从“../add rail/add rail.component”导入{AddRailComponent};
从'rxjs/Subject'导入{Subject};
从“@angular/core”导入{ApplicationRef};
从“../../services/user.service”导入{UserService};
@组成部分({
选择器:“应用程序站”,
templateUrl:'./station.component.html',
样式URL:['./station.component.css']
})
导出类StationComponent实现OnInit{
公共静态刷新站:主题=新主题();
站点:站点;//=新站点(268,‘站点名称’,null,null,[126127128],null);
轨道:轨道[]=[];
roleId:数字;
建造师(
私人站点服务:站点服务,
专用路由:激活的路由,
私人德拉古拉:德拉古拉服务,
专用utilsService:utilsService,
专用dialogService:dialogService,
私有appRef:ApplicationRef,
专用用户服务:用户服务){
StationComponent.refreshStation.subscribe(
res=>{
console.log('刷新…');
这个.getStation();
这是getRails();
}
);
}
恩戈尼尼特(){
这个.getStation();
这是getRails();
this.roleId=this.userService.getUsersRole().id;
这是dragula.drop.subscribe(
val=>{
this.station.railId=this.utilsService.map(this.rails,e=>e.railId);
this.stationService.saveRailOrder(this.station);
}
);
}
getStation(){
this.station=this.stationService.selected();
}
getRails(){
如果(this.station!=null){
this.stationService.getRails(this.station)
.订阅(
响应=>{
this.rails=响应;
},
err=>this.handleError(err)
);
}
}
手柄错误(err){
控制台日志(err);
}
showAddRail(){
const disposable=this.dialogService.addDialog(AddRailComponent,{station:this.station}).subscribe(resp=>
this.stationService.refresh()
);
}
}
我不确定我是否有足够的钱继续下去。我要检查的第一件事是确保所有服务都作为模块或appmodule中的提供者正确导入
Uncaught Error: Can't resolve all parameters for StationComponent: (?, [object Object], [object Object], [object Object], [object Object], [object Object], ?).
从错误中,“?”表示无法注入服务。因此,在您的例子中,“?”位于
StationService
和UserService
的位置。因此,请确保将这两项服务添加到app.module
中的providers
数组中。您是否将这两项服务添加到模块的providers
列表中?是的,它们都在这里:providers:[LoginService,StationService,UserService,ProfileService,LogoutService,RailService,RegisterService,GetStationService,TileService,TaskService,UtilService],看看。在中,J MADISON建议检查所有服务装饰符的拼写是否正确@Injectable()
,而不是Injectable()
或@Injectable
或@Injectable()
。有一个实例,在app.module.ts中更改用户服务的导入顺序可以找到它,但我无法对StationService执行相同的操作。。。