简体   繁体   English

在打字稿项目中使用 openlayers

[英]Use openlayers with typescript project

I'm trying to use new ol (>6.0.0) on my project with typescript.我正在尝试在我的带有打字稿的项目中使用 new ol (>6.0.0)。 In a previous versions I could just use it like this -在以前的版本中,我可以像这样使用它 -

///<reference path='../@types/openlayers/index.d.ts'/>

And use global ol object eg并使用全局 ol 对象,例如

ol.source.OSM()

But newer versions of ol don't really have this single object, but a number of modules.但是较新版本的 ol 并没有真正的单个对象,而是许多模块。 I try to reference path to folder with typings for new ol, but it doesn't really seem to work.我尝试使用新 ol 的类型引用文件夹的路径,但它似乎并没有真正起作用。 On the other hand, when I try to import them eg另一方面,当我尝试导入它们时,例如

import * as Map from 'ol/Map'

It says, that I have to use module:"system" or module:"AMD" in my typescript config.它说,我必须在我的打字稿配置中使用 module:"system" 或 module:"AMD"。 But is it the only way?但这是唯一的方法吗? My project structure is rather comples, and it would be hard to implement requirejs or systemjs.我的项目结构比较复杂,很难实现requirejs或systemjs。 What's the best solution in this case?在这种情况下,最好的解决方案是什么?

UPD: I've added systemJS at my index.html. UPD:我在 index.html 中添加了 systemJS。 But there's a huge problem - whenever I try to import my js file, using systemjs it works quite fine - I can use all functions which are declared inside of it and so on.但是有一个很大的问题 - 每当我尝试导入我的 js 文件时,使用 systemjs 它工作得很好 - 我可以使用在其中声明的所有函数等等。 However, when I include any import into my file (eg import {A} from "someconst"), systemjs stops working with tons of error messages.但是,当我在我的文件中包含任何导入时(例如从“someconst”导入 {A}),systemjs 停止处理大量错误消息。 And I'm completely confused why is it like that.我完全不明白为什么会这样。

The way you import has changed - do not use the ol namespace.您导入的方式已更改 - 不要使用ol命名空间。

Import the types explicitly:显式导入类型:

import { Map } from 'ol';
import { fromLonLat } from 'ol/proj';
import { Coordinate } from 'ol/coordinate';
// etc

Regarding typings, the official typings are not quite there yet, but I have found https://www.npmjs.com/package/@types/ol to work well.关于打字,官方打字还没有完全到位,但我发现https://www.npmjs.com/package/@types/ol 运行良好。

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

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