在 NavigationStart 的时候读 cookie
下面这样是可以的
import { DOCUMENT } from '@angular/common';
export class AppComponent {
constructor(
private router: Router,
@Inject(DOCUMENT) private document: any) {
router.events.subscribe(event => {
if (event instanceof NavigationStart) {
this.onNavigationStart(event);
}
});
}
async onNavigationStart(event: NavigationStart) {
if (this.document.cookie) {
console.log(this.document.cookie.indexOf(`${localcommon.Utils.CookieTokenKey}=`));
}
}
}
但是如果没有判断,就会报错 TypeError: Cannot read property 'indexOf' of undefined
async onNavigationStart(event: NavigationStart) {
console.log(this.document.cookie.indexOf(`${localcommon.Utils.CookieTokenKey}=`));
}
请教一下这是什么原因呢?