L не определено (пакет узла Leaflet.js) - PullRequest
0 голосов
/ 08 июня 2019

Я пытаюсь получить пример из руководства по быстрому запуску Leaflet для работы в Angular 7, но появляется ошибка ERROR ReferenceError: L is not defined.Примечание: я не включаю Leaflet через файлы JS, но установил его через npm через npm install leaflet, и он отображается в моем node_modules.

import { Component, OnInit } from '@angular/core';
declare let L;


@Component({
  selector: 'app-mapvisual',
  templateUrl: './mapvisual.component.html',
  styleUrls: ['./mapvisual.component.css']
})
export class MapvisualComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    const map = L.map('map').setView([51.505, -0.09], 13);

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '© <a href="https://www.openstreetmap.org    /copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
  }
}

РЕДАКТИРОВАТЬ: Я нашелрешение здесь.

1 Ответ

1 голос
/ 08 июня 2019

Вы должны включить эти ресурсы в свой index.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css"
    integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
    crossorigin=""/>

    <!-- Make sure you put this AFTER Leaflet's CSS -->
    <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
    integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
    crossorigin=""></script>
</head>
<body>

</body>
</html>

в вашем файле component.html

 <div id="mapid"></div>

component.css

#mapid { height: 180px; }

в вашем файле component.ts

import { Component, OnInit } from '@angular/core';
declare let L;


@Component({
  selector: 'app-mapvisual',
  templateUrl: './mapvisual.component.html',
  styleUrls: ['./mapvisual.component.css']
})
export class MapvisualComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    const map = L.map('mapid').setView([51.505, -0.09], 13);

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '© <a href="https://www.openstreetmap.org    /copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...