我正在使用路由器重用策略将以前的路由保存在基于Angular 6的应用程序中。我已将面包屑服务注入该自定义路由重用策略中。 当调用store方法来检查是否保存了先前的路由时,我正在调用面包屑服务的方法,该方法基本上会发出事件。 但是未订阅发出事件的组件,为什么?
我的问题:恢复以前的路线时,我们可以发出事件吗?如果是,那么如何?
routereuseservice.ts
// Router Class
export class RouteReuseService implements RouteReuseStrategy {
private handlers: { [key: string]: DetachedRouteHandle } = {};
constructor(private breadService:BreadCrumbService) {
}
//Store method which stores the route
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void
{
this.handlers[route.url.join("/") || route.parent.url.join("/")] = handle;
if(handle == null)
{
this.initbreadcrumb(route);
}
}
//method which is used to emit event
initbreadcrumb(route) {
this.breadService.event(data);
}
}
breadService.ts
// Bread Crumb Service
export class BreadCrumbService {
eventEmit = new EventEmitter();
event(response) {
//event emitter
this.eventEmit.emit(response);
}
getEmitter()
{
return this.eventEmit;
}
}
UserComponent.ts
//My Component
export class UserComponent {
ngOnInit()
{
//subscribing the emitted event
this.subscription = this.breadService.getEmitter().subscribe((item) => {
if(item!=undefined)
this.onEventHandler(item);
});
}
}
正在发出的事件未订阅。为什么?