我正在爲我的應用使用aurelia骨架打字稿webpack模板。 我想BingMaps添加到頁面,並能夠添加圖釘等 到目前爲止,我已經做到了這一點:如何使用BingMaps和Aurelia
index.html - I added a script tag that loads the map from CDN
<head>
...
<script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol'></script>
</head>
然後我加了這樣的一個模板:
map.html
<template>
<div id='mainMap' style='width: 100vw; height: 100vh;'></div>
</template>
然後我有地圖控制器:
map.ts
export class Map {
private map: Microsoft.Map.Map;
attached() {
this.map = new Microsoft.Maps.Map('mainMap', { credentials:'mycredentials - omitted'});
...
}
}
現在,如果我啓動應用程序,歡迎screent(從骨架)顯示。然後我點擊'地圖'菜單鏈接,地圖頁面顯示地圖完全加載。 但如果我點擊刷新瀏覽器(F5或Ctrl + F5)地圖不顯示了,並顯示在控制檯中的錯誤:
bluebird.js:1546 Unhandled rejection TypeError: Cannot read property 'prototype' of null at k (https://www.bing.com/mapspreview/sdk/mapcontrol:11:7096) at h (https://www.bing.com/mapspreview/sdk/mapcontrol:11:6285) at e (https://www.bing.com/mapspreview/sdk/mapcontrol:11:1106) at t.l [as instance] (https://www.bing.com/mapspreview/sdk/mapcontrol:11:161) at h (https://www.bing.com/mapspreview/sdk/mapcontrol:11:6042) at e (https://www.bing.com/mapspreview/sdk/mapcontrol:11:1106) at t.l [as instance] (https://www.bing.com/mapspreview/sdk/mapcontrol:11:161) at new Microsoft.Maps.Map (https://www.bing.com/mapspreview/sdk/mapcontrol:13:4304) at Map.attached (http://localhost:9000/app.bundle.js:31267:20) at Controller.attached (http://localhost:9000/aurelia.bundle.js:6438:22) at View.attached (http://localhost:9000/aurelia.bundle.js:4524:23) at ViewSlot.attached (http://localhost:9000/aurelia.bundle.js:4883:13) at View.attached (http://localhost:9000/aurelia.bundle.js:4534:19) at ViewSlot.attached (http://localhost:9000/aurelia.bundle.js:4883:13) at http://localhost:9000/aurelia.bundle.js:14717:28
這個錯誤被拋出時,它試圖實例化Map對象在地圖控制器的附加事件中。
爲什麼會發生這種情況,我該如何解決這個問題? 請幫助
感謝
這看起來像一個很好的解決方案,比我的解決方案更具可重用性,這是一個偉大的Aurelia方法! @盧卡,我想你應該嘗試這種方法。 – LStarky
謝謝@LStarky!只是知道我並沒有試圖對你發佈的解決方案說壞話,我只是指出他們仍然受到時間問題的困擾。我花了一段時間才弄清楚時間問題。 :-) –
我在回答問題時要做的重要事情是,即使開發人員想要在頁面上放置多個地圖,或者想要在應用程序的不同位置使用地圖,也能獲得可行的結果。但是,男人,我是否與突變觀察者走了一條瘋狂的路線。但是,嘿,我學會了如何使用該API,所以它不是完全浪費時間:-) –