如何在同一组件ts中的save()méthode中使用getComapny()?
CompanyUserDialogComponent.ts :
@Component({
selector: 'jhi-company-user-dialog',
templateUrl: './company-user-dialog.component.html'
})
export class CompanyUserDialogComponent implements OnInit {
constructor(
) {
}
save() {
console.log(this.user)
this.doNotMatch = null;
this.isSaving = true;
if (this.user.password !== this.confirmPassword) {
this.doNotMatch = 'ERROR';
}else if (this.user.id !== null) {
this.userService.update(this.user).subscribe((response) =>
this.onSaveSuccess1(response), () => this.onSaveError());
} else {
this.userService.create(this.user).subscribe((response) =>{
console.log(response);
this.onSaveSuccess1(response), () => this.onSaveError()
});
}
}
CompanyUserPopupComponent.ts :
@Component({
selector: 'jhi-company-user-popup',
template: ''
})
export class CompanyUserPopupComponent implements OnInit, OnDestroy {
a:any
routeSub: any;
constructor(
private route: ActivatedRoute,
private companyPopupService: CompanyPopupService,
private userModalService: UserModalService,
private companyService: CompanyService,
) {}
ngOnInit() {
this.routeSub = this.route.params.subscribe((params) => {
this.getComapny(params['id'])
if ( params['login'] ) {
this.userModalService.open(CompanyUserDialogComponent as Component, params['login']);
} else {
this.userModalService.open(CompanyUserDialogComponent as Component);
}
});
}
getComapny(id:number){
this.companyService.find(id).subscribe(res=>{
console.log(res)
})
}
ngOnDestroy() {
this.routeSub.unsubscribe();
}
}
答案 0 :(得分:0)
由于getCompany
中的CompanyUserPopupComponent
方法不依赖于组件,因此您只需在构造函数中注入服务即可使用getCompany
的{{1}}并使用CompanyService
CompanyUserDialogComponent.ts:
CompanyUserDialogComponent
希望这会有所帮助:)