【问题标题】:Get a number from a promise从 promise 中获取一个数字
【发布时间】:2016-05-23 12:38:14
【问题描述】:

我有:

 getZoom(): Promise<number> {
    return this._googleMapsApi.getMap().then((map:google.maps.Map) => map.getZoom());
}

我想检查缩放级别:

private onStartZoomEvent() {
    console.log('on start zoom');
    if (this._mapController.getZoom().valueOf() !== 12) {
    this.disableGeolocationButton();
    this._eventAggregator.trigger(this.disableGeolocation);
     }
}

我做错了什么?

【问题讨论】:

    标签: javascript typescript angular promise


    【解决方案1】:

    promise 的重点是您无法同步获取值。当您调用 getZoom() 时,它会返回一个承诺。然后,您尝试在该 Promise 上调用 valueOf();该方法不存在,并且承诺尚未实现。

    它应该看起来像这样:

    private onStartZoomEvent() {
        console.log('on start zoom');
        this._mapController.getZoom()
            .then((zoom) => {
                if (zoom.valueOf() !== 12) {
                this.disableGeolocationButton();
                this._eventAggregator.trigger(this.disableGeolocation);
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多