我正在构建angular2应用程序,目前我有一个带有菜单栏和路由器插座的主要内容。我添加了登录机制,因此如果用户未经过身份验证,则登录页面将显示在整个屏幕中,登录后用户将被路由到具有上述结构的主/ home组件。
当我运行应用程序时,登录页面加载,并且在成功通过身份验证后,它会将我路由到主页,但是在加载菜单的主页(如Dashboard1,Dashboard2,Report1等)中,链接是不正常。当我点击任何菜单栏链接时,我收到了下面提到的错误。
lib/@angular/platform-browser/bundles/platform-browser.umd.js:1900 Error: Uncaught (in promise): Error: Cannot match any routes: 'Report1'
at resolvePromise (zone.js:538)
at zone.js:515
at ZoneDelegate.invoke (zone.js:323)
at Object.onInvoke (lib/@angular/core/bundles/core.umd.js:9100)
at ZoneDelegate.invoke (zone.js:322)
at Zone.run (zone.js:216)
at zone.js:571
at ZoneDelegate.invokeTask (zone.js:356)
at Object.onInvokeTask (lib/@angular/core/bundles/core.umd.js:9091)
at ZoneDelegate.invokeTask (zone.js:355)

我有两个地方的路由器插座标签,即AppLoader中的1个,以及正在加载实际菜单的LayoutMenu组件,它包含用于加载主要内容的路由器插座。
路由器插座的正确位置是什么?我在做错的地方?
以下是routeconfig的代码
export const routes: RouterConfig = [
{ path: '', component: Login },
{ path: 'login', component: Login },
{ path: 'Home', component: Home },
{ path: '**', component: Login }
];
export const LOGIN_ROUTER_PROVIDERS = [
provideRouter(routes)
];
主页/菜单routeconfig如下
export const routes: RouterConfig = [
{
path: 'home',
component: Home,
// canActivate: [AuthenticationGuard],
children: [
{ path: 'Dashboard1', component: Dashboard1 },
{ path: 'Dashboard2', component: Dashboard2 },
{ path: 'Report1', component: Report1 },
{ path: 'Report2', component: Report1 },
{
path: 'ManageRedisCache',
component: ManageRedisCache,
children: [
{ path: '', component: ExtractorQueue },
{ path: 'Extractor', component: Extractor },
{ path: 'Schedule', component: Schedule },
{ path: 'CacheVisualization', component: CacheVisualization }
]
}
]
}
];
export const HOME_ROUTER_PROVIDERS = [
provideRouter(routes)
];
上面的2个routeconfig是通过main.ts bootstrap注入的。在main.ts bootstrap中,我还注入包含的AppLoader。
AppLoader组件如下所示,其中包含/加载登录页面。
@Component({
selector: 'my-app',
template: `<router-outlet></router-outlet>`,
directives: [Login, ROUTER_DIRECTIVES ]
})
export class AppLoader {
}
LayoutMenu组件如下:
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'MenuLayout',
template: `
<a [routerLink]="['/Dashboard1']"><h4>Dashboards 1</h4></a>
<a [routerLink]="['/Dashboard2']"><h4>Dashboards 2</h4></a>
<a [routerLink]="['/Report1']"><h4>Reports 1</h4></a>
<div class="outer-outlet">
<router-outlet></router-outlet>
</div> `,
directives: [LeftMenu, ROUTER_DIRECTIVES]
})
export class LayoutMenu { }
答案 0 :(得分:4)
Angular路由器非常灵活,您可以通过不同的配置获得相同的结果。贝娄是一个可能的例子。
您需要两个不同的基本模板:
示例主 app.component 路由配置:
export const routes: RouterConfig =
[
...ContentRoutes,
{
path: 'login',
component: LoginComponent
},
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: '**',
redirectTo: '/404',
pathMatch: 'full'
}
];
现在转到 content.component 路线(左侧菜单/右侧工作区的页面):
export const ContentRoutes: RouterConfig = [
{
path: 'home',
component: ContentComponent,
canActivate: [AuthenticationGuard],
children: [
{
path: '',
component: MainHomeComponent,
canActivate: [AuthenticationGuard]
}
]
},
{
path: '404',
component: ContentComponent,
canActivate: [AuthenticationGuard],
children: [
{
path: '',
component: PageNotFoundComponent,
canActivate: [AuthenticationGuard]
},
]
},
{
path: '500',
component: ContentComponent,
canActivate: [AuthenticationGuard],
children: [
{
path: '',
component: PageErrorComponent,
canActivate: [AuthenticationGuard]
},
]
},
];
所有 content.component 路由都应在路由前通过AuthenticationGuard
,这就是 canActivate 属性的原因。更多关于angular router docs。
我还添加了404/500路由示例。
打开模板。基本 app.component 模板只有RouterOutlet
:
<router-outlet></router-outlet>
它应该路由到 login.component 或 content.component (取决于用户身份验证)。
content.component 是您的“左侧菜单和工作区”模板,因此它应该包含基本布局组件和RouterOutlet
:
<div id="wrapper">
<div id="page-wrapper" class="gray-bg">
<div class="left-menu">
<app-menu></app-menu>
</div>
<router-outlet></router-outlet>
<app-footer></app-footer>
</div>
</div>
(示例html)
回顾:
基本路径('/')将重定向到'/ home',默认为 MainHomeComponent ,其中 ContentComponent 为父级。自由添加更多儿童路线。
github上的