typescript 宣传单“属性-路由-在类型-类型-导入时不存在”

k75qkfdt  于 7个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(84)

我发了一个帖子,但没有得到任何回应。我是新来的传单,不知道发生了什么或为什么。我相信我添加了所有的文件说要添加,但我仍然得到错误。

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>

import L from 'leaflet';

字符串
错误类型错误:无法读取未定义的属性“control”
我的代码:

var polylineRouteB = L.Routing.control({...});

xpcnnkqh

xpcnnkqh1#

为了让你的L.Routing类不被定义,你需要获取一个对map的引用,并在map加载时使用它,但在此之前,你需要配置angular.json来搜索你的assets文件夹中的标记图标,否则this issue arises

import { 
    latLng, 
    tileLayer, 
    Icon, icon, Marker
} from 'leaflet';

import 'leaflet';
import 'leaflet-routing-machine';
declare let L;
...

// Override default Icons
private defaultIcon: Icon = icon({
    iconUrl: 'assets/marker-icon.png',
    shadowUrl: 'assets/marker-shadow.png'
});

ngOnInit() {
    Marker.prototype.options.icon = this.defaultIcon;
}

onMapReady(map: L.Map) {
    L.Routing.control({
        waypoints: [
            L.latLng(57.74, 11.94),
            L.latLng(57.6792, 11.949)
        ],
        routeWhileDragging: true
    }).addTo(map);
}

字符串
模板

<div style="height: 800px;"
    leaflet 
    [leafletOptions]="options"
    (leafletMapReady)="onMapReady($event)">
</div>


Demo
还请注意,他的库不允许对服务器的无限请求,因此它经常返回429个HTTP响应。

zvokhttg

zvokhttg2#

你想安装这个:

npm install --save @types/leaflet-routing-machine

字符串
这是我的问题。

相关问题