简体   繁体   English

未捕获的TypeError:无法读取未定义的属性'navigateByUrl'

[英]Uncaught TypeError: Cannot read property 'navigateByUrl' of undefined

I'm adding multiple markers to Google Maps. 我正在向Google地图添加多个标记。 What I would like to happen is when the user clicks on the marker, it should navigate to the page where more information about that location is presented. 我想要发生的是当用户点击标记时,它应该导航到显示有关该位置的更多信息的页面。

I'm using Angular 4.2.4 and Google Maps JavaScript API (not SebastianM's angular-google-maps library). 我正在使用Angular 4.2.4和Google Maps JavaScript API(不是SebastianM的angular-google-maps库)。

Here's what I'm currently trying: 这是我目前正在尝试的内容:

// declare router and injected it into constructor
...

var googleMarker = customMarker.createMarker();
googleMarker.addListener('click', function () {
    console.log(`Clicked on marker`)
    this.router.navigateByUrl(`/drivers/${driver.id}`)
});

But it always returns the following error when clicked: 但是点击时它总是返回以下错误:

Uncaught TypeError: Cannot read property 'navigateByUrl' of undefined
    at _.Me.<anonymous> (map.component.ts:118)
    at Object._.A.trigger (js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:99)
    at BU.handleEvent (marker.js:51)
    at jy._.k.de (map.js:45)
    at jy._.k.Ki (map.js:43)
    at Object._.A.trigger (js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:99)
    at hq.<anonymous> (common.js:132)
    at Object._.A.trigger (js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:99)
    at hq._.k.Wi (common.js:192)
    at HTMLDivElement.<anonymous> (js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:36)
(anonymous) @ map.component.ts:118
_.A.trigger @ js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:99
BU.handleEvent @ marker.js:51
_.k.de @ map.js:45
_.k.Ki @ map.js:43
_.A.trigger @ js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:99
(anonymous) @ common.js:132
_.A.trigger @ js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:99
_.k.Wi @ common.js:192
(anonymous) @ js?key=AIzaSyDQ2QCrspYAltByRrP3vTN8OpSXcXZkbVk&libraries=geometry:36
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:424
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask @ zone.js:191
ZoneTask.invoke @ zone.js:486

What is the correct way to route to a given url when someone clicks on a marker? 当有人点击标记时,路由到给定网址的正确方法是什么?

EDIT - Here's what my component looks like: 编辑 - 这是我的组件的样子:

import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, RouterModule, Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';

import { Coordinate } from '../../models/coordinate.model';
import { CustomMarker } from '../../models/custom-marker.model';

declare var google: any;
declare var MarkerClusterer: any;

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

    // ...

    constructor(private db: AngularFireDatabase, private route: ActivatedRoute, private router: Router) { }

    ngOnInit() {
        // ...

        // Drivers
        this.drivers = this.db.list('/drivers');
        this.subDrivers = this.drivers.subscribe(snapshot => {
            this.stopsSnapshot = snapshot;

            // Calculate map bounds based on markers
            snapshot.forEach(driver => {
                // Add stop marker to map
                var customMarker = new CustomMarker(driver.color, 'driver', null, driver, this.map, "D");

                var googleMarker = customMarker.createMarker();
                googleMarker.addListener('click', function () {
                    this.router.navigateByUrl(`/drivers/${driver.id}`)
                });
            });
        });
    }

    // ...
}

Your error is located at googleMarker.addListener('click', function() {...} . using function() changes the context of the this keyword. 您的错误位于googleMarker.addListener('click', function() {...} 。使用function()更改this关键字的上下文。

Instead, use a fat arrow which preserves the context : googleMarker.addListener('click', () => {...} 相反,使用保留上下文的胖箭头: googleMarker.addListener('click', () => {...}

暂无
暂无

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

相关问题 Angular 6 Material - 无法读取未定义的属性“navigateByUrl” - Angular 6 Material - Cannot read property 'navigateByUrl' of undefined 未捕获的TypeError:无法读取未定义的属性“ core” - Uncaught TypeError: Cannot read property 'core' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;tb&#39; - Uncaught TypeError: Cannot read property 'tb' of undefined 未捕获的TypeError:无法读取未定义的属性“ chocolate” - Uncaught TypeError: Cannot read property 'chocolate' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;marksSession&#39; - Uncaught TypeError: Cannot read property 'marksSession' of undefined 未捕获(承诺中):TypeError:无法读取未定义的属性“userSubject”类型错误:无法读取未定义的属性“userSubject” - Uncaught (in promise): TypeError: Cannot read property 'userSubject' of undefined TypeError: Cannot read property 'userSubject' of undefined 错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ map” - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'map' of undefined 未捕获(承诺):类型错误:无法读取 angular2+ 中未定义的属性“get” - Uncaught (in promise): TypeError: Cannot read property 'get' of undefined in angular2+ 未捕获(承诺):类型错误:无法读取 ionic3 中未定义的属性“调用” - Uncaught (in promise): TypeError: Cannot read property 'call' of undefined in ionic3 未捕获的TypeError:无法读取未定义的属性'fn',电子2,角6? - Uncaught TypeError: Cannot read property 'fn' of undefined, electron 2, angular 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM