简体   繁体   English

如何包含打字稿openlayers 3外部模块?

[英]How to include typescript openlayers 3 external module?

I want to try setting up a simple map in openlayers using typescript: 我想尝试使用typescript在openlayers中设置一个简单的地图:

https://github.com/borisyankov/DefinitelyTyped/blob/master/openlayers/openlayers.d.ts https://github.com/borisyankov/DefinitelyTyped/blob/master/openlayers/openlayers.d.ts

I take that file, put it into 我拿那个文件,把它放进去

/typings/openlayers/openlayers.d.ts

I have an app.ts file at the top in my root directory that I put: 我在我的根目录的顶部有一个app.ts文件:

import { Map } from "olx"; 从“olx”导入{Map};

In my tsconfig.json, I've included the path to the openlayers.d.ts file. 在我的tsconfig.json中,我已经包含了openlayers.d.ts文件的路径。 But I am gettting olx is not defined when I use the tsc command from npm's typescript package. 但是我在使用npm的typescript包中的tsc命令时没有定义olx。

All I am going for is drawing a simple openlayers3 map, hope to not deviate from javascript too much if possible. 我想要的只是绘制一个简单的openlayers3地图,希望如果可能的话不要过多地偏离javascript。

First install openlayers: 首先安装openlayers:

npm install openlayers

Then install types openlayers to your dev env: 然后在开发环境中安装类型openlayers:

npm install --save-dev @types/openlayers

To import I tried: 要导入我试过:

import * as ol from 'openlayers';

Then you can invoke anything in openlayers like this: 然后你可以像这样在openlayers中调用任何东西:

new ol.Map();

import { Map } from "olx"; 从“olx”导入{Map};

This will not work as there is no ambient external module called "olx" . 这不起作用,因为没有名为"olx"环境外部模块。 also olx refers to something else on NPM https://www.npmjs.com/package/olx so we recommend not using this name. 另外, olx指的是NPM上的其他内容https://www.npmjs.com/package/olx,因此我们建议不要使用此名称。

Recommend : https://www.npmjs.com/package/openlayers 推荐: https//www.npmjs.com/package/openlayers

With a definition file extended as: 定义文件扩展为:

declare module "openlayers" {
    export = olx;
}

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

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