简体   繁体   English

角度-无法使用Bing Map图钉单击重定向到另一个组件

[英]Angular - Not able to redirect to another component using Bing Map push pin click

Bing Map Push pin is clicked, but Router gets undefined in the console log. 单击“必应地图推针”,但在控制台日志中未定义路由器。

so unable to redirect. 因此无法重定向。 If I added router.navigate in the constructor it is working perfectly. 如果我在构造函数中添加了router.navigate,则它工作正常。

Below is my code 下面是我的代码

  import { Component, ViewChild, OnInit } from '@angular/core'; import { } from "bingmaps/scripts/MicrosoftMaps/Microsoft.Maps.All" import {Router,ActivatedRoute} from '@angular/router'; @Component({ selector: 'device-map', templateUrl: './device-map.component.html' }) export class DeviceMapComponent implements OnInit{ @ViewChild('myMap') myMap; constructor(public router: Router, public route: ActivatedRoute,) { console.log(this.router); } pushpinClicked(e){ console.log("I am clicked"); console.log(this.router); this.router.navigate(['/Dashboard'], { queryParams: { deviceId: e.target.metadata.title } }); } ngOnInit(){ var map = new Microsoft.Maps.Map(this.myMap.nativeElement, { credentials: 'My key', center: new Microsoft.Maps.Location(47.6149, -122.1941) }); var infobox = new Microsoft.Maps.Infobox(map.getCenter(), { title: "Microsoft", description: "City Center", visible: false, }); infobox.setMap(map); var randomLocation = Microsoft.Maps.TestDataGenerator.getLocations(1, map.getBounds()); var pin = new Microsoft.Maps.Pushpin(map.getCenter(), { title: 'Microsoft' }); pin.metadata = { title: 'Device 1', description: 'Pin discription' }; Microsoft.Maps.Events.addHandler(pin, 'click', this.pushpinClicked); map.entities.push(pin); } } 

Below is a way to add navigation for push pin click (Bing map). 以下是为图钉单击添加导航的方法(必应地图)。 Arrow function is required. 需要箭头功能。

Microsoft.Maps.Events.addHandler(pin, 'click',  () => {
 this.router.navigateByUrl('/dashboard');});

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

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