【发布时间】:2019-02-13 17:59:15
【问题描述】:
实际日志顺序: ('ngOnInit 开始') ('在我之后 aaya',this.policydetails) ('这里', Object.keys(this.policy).length)
预期的日志顺序: ('ngOnInit 开始') ('这里', Object.keys(this.policy).length) ('在我之后',this.policydetails)
Component.ts 文件 sn-p 下面:
ngOnInit() {
console.log('ngOnInit started');
this.route.params.subscribe(params => {
this.getPoliciesService.getPolicyDetails(params.policyNo)
.subscribe((data: PoliciesResponse) => {
this.policy = data.data[0];
this.flattenPolicy();
console.log('Here', Object.keys(this.policy).length);
});
});
this.makePolicyTable();
}
ngAfterViewInit() {
console.log('after me aaya', this.policydetails);
const table = this.policydetails.nativeElement;
table.innerHTML = '';
console.log(table);
console.log(this.table);
table.appendChild(this.table);
console.log(table);
}
Service.ts文件sn-p如下:
getPolicyDetails(policyNo) {
const serviceURL = 'http://localhost:7001/getPolicyDetails';
console.log('getPolicyDetails service called, policyNo:', policyNo);
const params = new HttpParams()
.set('policyNo', policyNo);
console.log(params);
return this.http.get<PoliciesResponse>(serviceURL, {params} );
}
下面API调用对应的JS文件sn-p:
router.get('/getPolicyDetails', async function(req, res) {
let policyNo = (req.param.policyNo) || req.query.policyNo;
console.log('policyNo', typeof policyNo);
await helper.getPolicyDetails({'policyNo' : policyNo},
function(err, data) {
console.log(err, data)
if (err) {
return res.send({status : false, msg : data});
}
return res.send({status : true, data : data});
});
});
谁能建议我需要在哪里异步等待预期的日志顺序?
【问题讨论】:
-
getPolicyDetails 您只能在 ngAfterViewInit 内部调用。无需 2 个生命周期
标签: node.js angular typescript angular6