我尝试在ngOnInit
方法中为每个方法执行一个(我使用Angular 4+)。
但它没有执行
有我的代码:
export class ObjectDetailComponent implements OnInit {
public object$: Object ;
private member$: Object ;
private _sellers = [];
private _seller: string;
constructor(private route: ActivatedRoute, private _cookieService: CookieService,
private _objectService: AuctionedObjectService, private _memberService: MemberService, private _sellerService: SellerService) {
this.route.params.subscribe( params => this.object$ = params.id );
}
ngOnInit() {
this._objectService.getObject(this.object$).subscribe(
data => this.object$ = data);
this._memberService.getMember(Number(this._cookieService.get('login')))
.subscribe(data2 => this.member$ = data2);
this._sellerService.getSellers()
.subscribe(data3 => this._sellers = data3);
this.sellerUser();
}
public sellerUser(){
for(const seller of this._sellers){
if(Number(this._cookieService.get('login')) === seller.idUser){
this._seller = seller.username;
break;
}
}
}
}
sellerUser()
不起作用:/
答案 0 :(得分:0)
确定获得结果后,请使用同步逻辑,并在this.sellerUser();
下使用Subscripition
。在您当前的实现中,this.sellerUser();
将在您的订阅结束之前被调用(coz订阅是异步的,将在执行所有同步代码后最后执行)
this._sellerService.getSellers()
.subscribe(data3 => {
this._sellers = data3;
this.sellerUser(); // here
});