【问题标题】:Angular 2 Routing Children and router-outletAngular 2 路由子节点和路由器出口
【发布时间】:2017-12-23 04:52:40
【问题描述】:

我正在使用带有子节点的路由,这些子节点应该加载到单独的插座中。 所有组件都直接放在 /app 文件夹中。

app.router.ts:

export const router: Routes = [

    { path:'dashboard', canActivate:[AuthguardGuard], component: DashboardComponent },
    { path:'', component: LoginComponent },
    { 
        path: 'dashboard', component: DashboardComponent, 
        children: [
           { path:'features', component: FeaturesComponent, outlet: 
           { path:'signup', component: SignupComponent, outlet: 'content' },
           { path:'more', component: MoreComponent, outlet: 'content', 
           { path: '**', component: PageNotFoundComponent }
       ]
    }
];

dashboard.component.html:

<a [routerLink]="['./']">Overview</a>
<a [routerLink]="['features']">Features</a>
<a [routerLink]="['details']">How To</a>
<a [routerLink]="['more']">SignUp</a>
<a [routerLink]="['signup']">SignUp</a>

当在http.//localhsot:4200 下加载应用程序时,我首先从一个登录组件/页面(默认加载在未命名的路由器插座中)开始检查用户并通过。之后它进入仪表板组件/页面(使用 AuthguardGuard)。网址为:http://localhost:4200/dashboard 从仪表板页面我希望能够导航到其他页面/组件,它们应该加载到插座中:“内容”

app.component.ts:

<main class="mdl-layout__content">
    <div class="mdl-layout__tab-panel is-active" id="overview">
        <router-outlet></router-outlet>
        <router-outlet name="content"></router-outlet>
    </div>
</main>

我一直在尝试在平台上找到的一些建议,但都没有帮助。例如。: <a [routerLink]="['signup', {outlets: {content: 'signup'}}]">SignUp</a> <a [routerLink]="['../signup']">SignUp</a>

但它不起作用......不同的错误,如: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'signup'

任何想法我做错了什么或者我应该改变或添加什么?

【问题讨论】:

  • 我的回答解决了你的问题吗?
  • 很遗憾不是.. 同样的错误:Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'signup'
  • 你有没有找到任何解决方法,因为我遇到了同样的问题。但对我来说 ['/dashboard',{outlets: {content:'signup'}}] 有效。但它创建了一个我无法使用的 URL。
  • @PraveenRawat:是的,现在工作正常。您能否发布您无法使用的生成的 URL。这会很有帮助。

标签: angular angular2-routing


【解决方案1】:

我认为解决这个问题非常困难。尝试像这样在您的路由器链接中定义一个空路径

[routerLink]="['', {outlets: {content: 'signup'}}]">SignUp</a>

您在 app.component.html 中定义了您的命名出口,这就是路径为空的原因

更新 1

命名路由器没有与您的子路由一起显示的原因是因为您的命名路由器与您的仪表板在同一个模板中,就像这样

 <router-outlet></router-outlet>
 <router-outlet name="content"></router-outlet>

这里的错误是您的“内容”路由器与显示仪表板的“主”路由器处于同一级别。 “内容”路由器应该在dashboard.component.html 中,而NOT 在你的app.component.html 中。只需将&lt;router-outlet name="content"&gt;&lt;/router-outlet&gt; 复制粘贴到您的dashboard.component.html 中即可。

【讨论】:

  • 在没有子部分的情况下尝试您的建议,空路径适用于名为“content”的插座。这是我所做的更改:export const router: Routes = [ { path:'signup', component: SignupComponent, outlet: 'content' }, { path:'', component: LoginComponent }, { path: 'dashboard', canActivate:[AuthguardGuard], component: DashboardComponent, children: [ { path:'features', component: FeaturesComponent, outlet: .... ] } ]; 但我想和孩子们一起解决它……还有其他解决方案吗?
  • @k.vincent 这是可能的。等待我的回答
猜你喜欢
  • 2023-03-12
  • 2017-12-03
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 2017-03-04
  • 2017-07-04
  • 2018-09-12
  • 1970-01-01
相关资源
最近更新 更多