我如何在 Angular2 应用程序中 embed/isolate JS 组件(Google 地图)?
How do I embed/isolate JS components (Google Maps) in an Angular2 app?
在尝试将 Google 地图 (API v3) 小部件嵌入 Angular2 应用程序时,我发现有一个 click/drag 错误困扰着我。
我想知道是否有一种好方法可以嵌入与 Angular 隔离的 Google 地图组件。根据堆栈跟踪(如下),在 Zone 尝试调用任务后,Google Maps 似乎出现了意外情况。
简单的 Plunkr 示例(可运行):https://plnkr.co/edit/Tg3zwphygrgLUG5mq8Cc
每次错误点击后回溯(README.md):
- Angular: angular2-polyfills.js:324 Uncaught TypeError: Cannot read property 'Ra' of null
- Google Maps: _.r.jl @ common.js:244
- Google Maps: _.J.trigger @ js?key=NICE_TRY_HACKERS&callback=initMap:103
- Google Maps: fx @ common.js:190
- Google Maps: (anonymous function) @ common.js:189
- Google Maps: _.J.trigger @ js?key=NICE_TRY_HACKERS&callback=initMap:103
- Google Maps: _.r.Mm @ common.js:257_.r.Kn @ common.js:231
- Angular: ZoneDelegate.invokeTask @ angular2-polyfills.js:423
- Angular: Zone.runTask @ angular2-polyfills.js:320
- Angular: ZoneTask.invoke @ angular2-polyfills.js:490
HTML (index.html):
<!--
Google Maps alongside Angular2
=======================================
See README.md
-->
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 1. (Angular2) Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system-polyfills.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2-polyfills.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system.src.js"></script>
<script src="https://npmcdn.com/rxjs@5.0.0-beta.2/bundles/Rx.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2.min.js"></script>
<!-- 2. (Angular2) Configure SystemJS -->
<script>
System.import('main.js').then(null, console.error.bind(console));
</script>
</head>
<!-- 3. (Angular2) Display the application -->
<body>
<my-app>Loading...</my-app>
<!-- 4. GOOGLE MAPS START, source: https://developers.google.com/maps/documentation/javascript/tutorial#The_Hello_World_of_Google_Maps_v3-->
<style>
html, body, #map {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<div id="map"></div> <!-- Google Map goes here -->
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=NOTHING_TO_SEE_HERE&callback=initMap" async defer></script>
<!-- GOOGLE MAPS END -->
</body>
</html>
Angular2 基本应用程序 (main.ts)
import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';
// Create Angular component
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
// Bootstrap the component
bootstrap(AppComponent);
观察:
- 每次点击都会触发错误
- 错误似乎并不严重,但最好不要在摇摇欲坠的基础上继续
- Stacktrace 建议 Google Maps (_.r.jl @ common.js:244) 对 Angular2 的 Zone (ZoneDelegate.invokeTask @ angular2 调用的任务感到惊讶-polyfills.js)
- 该小部件在基本应用程序中运行,但在 Angular
中出现问题
很少有 3rdparty 解决方案提供像 https://angular-maps.com/ 这样随时可用的组件。
在尝试将 Google 地图 (API v3) 小部件嵌入 Angular2 应用程序时,我发现有一个 click/drag 错误困扰着我。
我想知道是否有一种好方法可以嵌入与 Angular 隔离的 Google 地图组件。根据堆栈跟踪(如下),在 Zone 尝试调用任务后,Google Maps 似乎出现了意外情况。
简单的 Plunkr 示例(可运行):https://plnkr.co/edit/Tg3zwphygrgLUG5mq8Cc
每次错误点击后回溯(README.md):
- Angular: angular2-polyfills.js:324 Uncaught TypeError: Cannot read property 'Ra' of null
- Google Maps: _.r.jl @ common.js:244
- Google Maps: _.J.trigger @ js?key=NICE_TRY_HACKERS&callback=initMap:103
- Google Maps: fx @ common.js:190
- Google Maps: (anonymous function) @ common.js:189
- Google Maps: _.J.trigger @ js?key=NICE_TRY_HACKERS&callback=initMap:103
- Google Maps: _.r.Mm @ common.js:257_.r.Kn @ common.js:231
- Angular: ZoneDelegate.invokeTask @ angular2-polyfills.js:423
- Angular: Zone.runTask @ angular2-polyfills.js:320
- Angular: ZoneTask.invoke @ angular2-polyfills.js:490
HTML (index.html):
<!--
Google Maps alongside Angular2
=======================================
See README.md
-->
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 1. (Angular2) Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system-polyfills.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2-polyfills.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system.src.js"></script>
<script src="https://npmcdn.com/rxjs@5.0.0-beta.2/bundles/Rx.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2.min.js"></script>
<!-- 2. (Angular2) Configure SystemJS -->
<script>
System.import('main.js').then(null, console.error.bind(console));
</script>
</head>
<!-- 3. (Angular2) Display the application -->
<body>
<my-app>Loading...</my-app>
<!-- 4. GOOGLE MAPS START, source: https://developers.google.com/maps/documentation/javascript/tutorial#The_Hello_World_of_Google_Maps_v3-->
<style>
html, body, #map {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<div id="map"></div> <!-- Google Map goes here -->
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=NOTHING_TO_SEE_HERE&callback=initMap" async defer></script>
<!-- GOOGLE MAPS END -->
</body>
</html>
Angular2 基本应用程序 (main.ts)
import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';
// Create Angular component
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
// Bootstrap the component
bootstrap(AppComponent);
观察:
- 每次点击都会触发错误
- 错误似乎并不严重,但最好不要在摇摇欲坠的基础上继续
- Stacktrace 建议 Google Maps (_.r.jl @ common.js:244) 对 Angular2 的 Zone (ZoneDelegate.invokeTask @ angular2 调用的任务感到惊讶-polyfills.js)
- 该小部件在基本应用程序中运行,但在 Angular 中出现问题
很少有 3rdparty 解决方案提供像 https://angular-maps.com/ 这样随时可用的组件。