【发布时间】:2021-04-21 03:50:15
【问题描述】:
这是我的代码,可以正常工作,但很难看。我的目标是在服务中做到这一点。但首先当我把异步等待功能不起作用:
看看这个:
ngOnInit() {
this.getLocation();
}
getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
this.position = position;
this.makeSomething_1();
this.makeSomething_3();
this.makeSomething_4();
}, positionError => {
console.log('The user don accept location.');
this.makeSomething_1();
this.makeSomething_3();
this.makeSomething_4();
});
} else {
console.log('Geolocation is not supported by this browser.');
this.makeSomething_1();
this.makeSomething_3();
this.makeSomething_4();
}
}
但我想做这样的事情:
ngOnInit() {
this.getLocation();
this.makeSomething_1();
this.makeSomething_3();
this.makeSomething_4();
}
async getLocation() {
if (navigator.geolocation) {
await navigator.geolocation.getCurrentPosition(position => {
this.position = position;
}, positionError => {
console.log('The user don accept location.');
});
} else {
console.log('Geolocation is not supported by this browser.');
}
}
我想要的问题是this.position print "undefined"
有人可以教我如何编写好的代码吗?
请不要给我-1。我尽力了
【问题讨论】:
标签: angular async-await