【问题标题】:Promises not working after re entering the page重新进入页面后承诺不起作用
【发布时间】:2017-08-13 10:45:42
【问题描述】:

我有一个包含 2 页的 Ionic 2 应用程序。当我点击主页上的一个按钮时,它会将我带到一个页面,其中我们有一张地图,可以定位当前位置和附近的位置。显示所有位置,但是当我返回主页并重新单击按钮时,第二页中的地图仅显示当前位置而不是附近。 这是初始化第二页的代码:

ionViewDidLoad(){

this.platform.ready().then(() => {
  let mapLoaded = this.maps.init(this.mapElement.nativeElement, this.pleaseConnect.nativeElement);
  let locationsLoaded = this.locations.load();

  Promise.all([
    mapLoaded,
    locationsLoaded
  ]).then((result) => {
    let locations = result[1];

    for(let location of locations){
      this.maps.addMarker(location.latitude, location.longitude,'http://maps.google.com/mapfiles/ms/icons/blue-dot.png');
    }

  });

});

【问题讨论】:

  • 你在ionViewWillEnter中试过你的功能了吗?

标签: angular typescript ionic2 promise


【解决方案1】:

尝试在路由中使用可观察对象;

constructor(router:Router) {
  router.events.subscribe(event:Event => {
    if(event instanceof NavigationStart) {
    }
    // NavigationEnd
    // NavigationCancel
    // NavigationError
    // RoutesRecognized
  });
}

您也可以尝试阅读这篇文章;可能会有所帮助。 https://angular-2-training-book.rangle.io/handout/observables/observables_array_operations.html

【讨论】:

  • 谢谢!您能否向我解释一下这段代码以及如何将我的代码更改为您的代码,因为我是 Angular 新手??
  • 你需要把它放在你的路由器里,如果你不确定那是什么,如果你还没有这样做,请按照本教程angular.io/docs/ts/latest/tutorial
  • 我也是 angular2 的新手,这极大地帮助了我的理解,在侧面菜单的同一页面上,您可以找到“cookbook”并查看“router”和“observables”教你如何但是,这样做超出了您的问题范围。并且已经在这里回答了几次我确定。
  • 如果这有助于您发现问题,或者将您引向不同的问题,请不要忘记标记我的答案 :)
  • Ok James Warren,我会试试这个,稍后再回答你。
猜你喜欢
  • 2017-08-03
  • 2020-10-25
  • 2017-04-29
  • 2016-06-07
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 2017-08-07
  • 2016-08-15
相关资源
最近更新 更多