简体   繁体   English

使用javascript扩展typescript库

[英]Extends typescript library with javascript

I'm working with Ionic 3 to build a simple map application, in order to present the map I'm using leaflet (typed version) with the OpenStreet map tiles 我正在使用Ionic 3来构建一个简单的地图应用程序,以便使用OpenStreet地图图块呈现我正在使用传单 (类型版本)的地图

I want to give the possibility to the user to download and cache the map, thus I found leaflet-offline here and wanted to use it since it allows me to use my localstorage to store the images. 我想让用户下载和缓存地图的可能性,因此我在这里找到了传单离线并希望使用它,因为它允许我使用我的本地存储来存储图像。

Now my problem arise with the fact that I'm trying to mix typescript with javascript and I can not figure out how to make it work correctly. 现在我的问题出现了,我正在尝试将打字稿与javascript混合,我无法弄清楚如何使其正常工作。

What I have done and what is the problem: 我做了什么,问题是什么:

I installed leaflet typed version and then leaflet-offline javascript version. 我安装了传单类型版本,然后安装了传单离线javascript版本。

Now I imported them as follows inside my page: 现在我在页面中将它们导入如下:

import * as leaflet from 'leaflet';
import 'leaflet-offline';

Now if I try to use the library as explained in the example I get a typescript error which indicates me that the property offline is not in the object TileLayer. 现在,如果我尝试按照示例中的说明使用库,我会得到一个打字稿错误,它指示我离线的属性不在对象TileLayer中。

leaflet.tileLayer.offline('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', tilesDb, {
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
    subdomains: 'abc',
    minZoom: 13,
    maxZoom: 19,
    crossOrigin: true
}).addTo(this.map);

In order to avoid this I added 为了避免这种情况,我补充道

export namespace tileLayer {
function wms(baseUrl: string, options?: WMSOptions, offline?: any): TileLayer.WMS;
function offline(url?: any, tilesDb?: any, options?: any): any;
}

the function offline is in the namespace of tileLayer itself. offline函数位于tileLayer本身的命名空间中。

Current problem At the moment when I try to run the application everything works fine except for the actual map, I can't see it and the error that I get when I look into it is the following: 当前的问题在我尝试运行应用程序的那一刻,一切正常,除了实际的地图,我看不到它,当我调查它时得到的错误如下:

ERROR TypeError: Cannot read property 'then' of null at NewClass.getTileUrl (vendor.js:163411) at NewClass.createTile (vendor.js:163389) at NewClass._addTile (vendor.js:79419) at NewClass._update (vendor.js:79310) at NewClass._setView (vendor.js:79171) at NewClass._resetView (vendor.js:79129) at NewClass.fire (vendor.js:68787) at NewClass._move (vendor.js:72343) at NewClass._onZoomTransitionEnd (vendor.js:72800) at NewClass._catchTransitionEnd (vendor.js:72732) 错误类型错误:无法在NewClass._TateUrl(vendor.js:163489)的NewClass._addTile(vendor.js:79419)的NewClass._update(供应商,js:73419)的NewClass.getTileUrl(vendor.js:163489)中读取null的属性'then'(供应商) .js:79310)在NewClass._setView(vendor.js:79171)的NewClass._resetView(vendor.js:79129)NewClass.fire(vendor.js:68787)NewClass._move(vendor.js:72343)at at NewClass._catchTransitionEnd中的NewClass._onZoomTransitionEnd(vendor.js:72800)(vendor.js:72732)

I'm sure I'm doing some stupid mistake, but I lost a lot of time on this issue. 我确定我犯了一些愚蠢的错误,但我在这个问题上失去了很多时间。

Anyone have some clue on how to go solve this? 任何人都有一些关于如何解决这个问题的线索?

You can see in the source https://github.com/robertomlsoares/leaflet-offline/blob/master/src/TileLayer.Offline.js 您可以在源代码中看到https://github.com/robertomlsoares/leaflet-offline/blob/master/src/TileLayer.Offline.js

that you fail at: 你失败了:

    var resultPromise = this._tilesDb.getItem(dbStorageKey).then(function (data) {

So the call to 所以打电话给

_tilesDb.getItem(dbStorageKey)

is returning null, at which point the code tries to call 'then' on null. 返回null,此时代码尝试在null上调用'then'。

Since the tilesDB is something that you are supposed to be providing yourself, you can put a breakpoint in your getItem function to see what is going on. 由于tilesDB是您应该自己提供的东西,因此您可以在getItem函数中放置一个断点来查看正在发生的事情。 At some point you are returning null from your getItem function. 在某些时候,您将从getItem函数返回null。

What getItem is supposed to do: getItem应该做什么:

// return Promise that has the image Blob/File/Stream.

The key supplied to getItem is the url you supplied with a regex substitution applied to it (see _getStorageKey in same source file) 提供给getItem的密钥是你提供的应用了正则表达式替换的url(参见同一源文件中的_getStorageKey)

Cannot read property 'then' of null 无法读取属性'then'的null

Typescript gives this error when then receives null from the function it's applied on. 打字稿给出了这样的错误时, then接收null从它的应用上的功能。 So check that you are not missing return statement if your function is custom. 因此,如果您的函数是自定义的,请检查您是否缺少return语句。

Now instead of adding layer to map directly at the time of say initialization. 现在,而不是在初始化时直接添加图层来映射。 for that try to replace your existing code with 为此尝试用你的现有代码替换

const offlineLayer = leaflet.tileLayer.offline('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', tilesDb, {
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
    subdomains: 'abc',
    minZoom: 13,
    maxZoom: 19,
    crossOrigin: true
});

offlineLayer.addTo(this.map)

check the reference of the map either this.map or whatever the refernce in your code. 检查地图的引用this.map或代码中的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM