ObjectUnsubscribedError when navigating in event handler

时间:2016-04-25 09:34:28

标签: angular angular2-routing

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?

1 个答案:

答案 0 :(得分:1)

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.