我有一個奇怪的問題。我有一個基本的路由配置是這樣的:Angular:無法讀取null的屬性'component'
const appRoutes: Routes = [
{path: '', redirectTo: '/search', pathMatch: 'full'},
{path: 'login', component: LoginComponent },
{path: 'dashboard', component: DashboardComponent, canActivate: [OnlyLoggedInGuard]},
{path: 'search', component: SearchComponent, canActivate: [OnlyLoggedInGuard]}
];
在應用HTML,我導入有條件的路由器出口:
<div *nfIf="condition">
...
<router-outlet></router-outlet>
...
</div>
<div *nfIf="!condition">
... another dom construction
<router-outlet></router-outlet>
...
</div>
我攔截(與HttpInterceptor
)HTTP 401錯誤重定向到登錄頁。在我的HTTP攔截某些時候這意味着我有這樣的:
this._router.navigate(['/login', {error:'I am an error message'}]);
當我在儀表盤和401時,我重定向正確與正確的錯誤參數到登錄頁面。 然而,當我在登錄頁上,我得到我有以下錯誤的服務器401:
core.es5.js:1020 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'component' of null
TypeError: Cannot read property 'component' of null
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverseRoutes (router.es5.js:4343)
at router.es5.js:4305
at Array.forEach (<anonymous>)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverseChildRoutes (router.es5.js:4304)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverse (router.es5.js:4261)
at MapSubscriber.project (router.es5.js:4100)
at MapSubscriber.webpackJsonp.../../../../rxjs/operator/map.js.MapSubscriber._next (map.js:77)
at MapSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/operator/mergeMap.js.MergeMapSubscriber.notifyNext (mergeMap.js:143)
at InnerSubscriber.webpackJsonp.../../../../rxjs/InnerSubscriber.js.InnerSubscriber._next (InnerSubscriber.js:23)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverseRoutes (router.es5.js:4343)
at router.es5.js:4305
at Array.forEach (<anonymous>)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverseChildRoutes (router.es5.js:4304)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverse (router.es5.js:4261)
at MapSubscriber.project (router.es5.js:4100)
at MapSubscriber.webpackJsonp.../../../../rxjs/operator/map.js.MapSubscriber._next (map.js:77)
at MapSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/operator/mergeMap.js.MergeMapSubscriber.notifyNext (mergeMap.js:143)
at InnerSubscriber.webpackJsonp.../../../../rxjs/InnerSubscriber.js.InnerSubscriber._next (InnerSubscriber.js:23)
at resolvePromise (zone.js:783)
at resolvePromise (zone.js:754)
at zone.js:831
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
at Object.onInvokeTask (core.es5.js:3881)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:191)
at drainMicroTaskQueue (zone.js:595)
at ZoneTask.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
at invokeTask (zone.js:1370)
當您打開應用程序時,服務器也會拋出401 –
我剛剛嘗試執行操作(例如:登錄)時收到401錯誤。不是當我加載頁面開始。 –
如果刪除搜索路徑上的'canActivate',錯誤是否會消失? –