简体   繁体   中英

ObjectUnsubscribedError when navigating in event handler

I got a navigation service

import { Injectable, Output, EventEmitter } from 'angular2/core';

@Injectable()
export class NavigationService
{
    @Output() navigating = new EventEmitter();
    navigate(param: number) {
    this.navigating.emit({ param})
    }
}

which I subscribe for in my app.component

export class AppComponent implements OnInit {
    constructor(private _router: Router,
        private _navigationService: NavigationService) {
    }
    ngOnInit() {
    this._navigationService.navigating
        .subscribe(data => {
            this._router.navigate(['SomeView', { param: data.param}]);
        })
    }
}

Navigation actually happens, but on the console I get this error:

EXCEPTION: Error: Uncaught (in promise): ObjectUnsubscribedError browser_adapter.ts:73

(cut for brevity).

I don't know what to make of this, my guess is that 'AppComponent' is unsubscribed from navigating , but that shouldn't matter should it?

You don't need to use the Output decorator in services:

@Output() navigating = new EventEmitter();

Should be simply:

navigating = new EventEmitter();

The @Ouptut decorator is for components that want to expose custom events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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