【发布时间】:2018-10-24 10:26:54
【问题描述】:
我想监听NavigationStart事件,判断其url属性是否为/logout。
如果是这样,当我的侦听器检测到正确的NavigationStart 事件。
否则路由器应该继续路由。
我不想使用组件,因为我不会显示任何 /logout 视图。
import { Component, OnInit } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnInit {
constructor(
private router: Router,
) {
router.events.filter(event => event instanceof NavigationStart).subscribe(
(event: NavigationStart) => {
if (event.url == '/logout') {
this.logout();
// TODO stop firing successive events
}
}
);
}
ngOnInit() { }
logout() {
// TODO logout operations
}
}
【问题讨论】:
-
您必须将您的用户重定向到某个地方,否则,他会留在经过身份验证的用户的视图中。您不能将用户重定向到登录视图吗?目前还不清楚你想要实现什么。
标签: angular angular2-routing angular2-directives