Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angular2 CLI中声明默认路由?_Angular_Routing_Routes_Angular2 Cli - Fatal编程技术网

如何在Angular2 CLI中声明默认路由?

如何在Angular2 CLI中声明默认路由?,angular,routing,routes,angular2-cli,Angular,Routing,Routes,Angular2 Cli,我使用angular CLI做了一个小项目,但我不知道如何在CLI中声明默认路由。我将“/”用于父组件,它可以工作,但如果我将“/”用于子组件,它就不能工作 我的父.component.ts是: @Routes([ { path: '/', component: LoginComponent }, ]) <a [routerLink]="['/Login']"></a> <router-outlet></router-outlet>

我使用angular CLI做了一个小项目,但我不知道如何在CLI中声明默认路由。我将“/”用于父组件,它可以工作,但如果我将“/”用于子组件,它就不能工作

我的父.component.ts是:

@Routes([
  { path: '/', component: LoginComponent },
])   
<a [routerLink]="['/Login']"></a>
<router-outlet></router-outlet>
@Routes([
    { path: '/', component: DashboardComponent},
])
<li class="treeview">
    <a [routerLink]="['']">Dashboard</a>
</li>
<router-outlet></router-outlet>
parent.component.html是:

@Routes([
  { path: '/', component: LoginComponent },
])   
<a [routerLink]="['/Login']"></a>
<router-outlet></router-outlet>
@Routes([
    { path: '/', component: DashboardComponent},
])
<li class="treeview">
    <a [routerLink]="['']">Dashboard</a>
</li>
<router-outlet></router-outlet>
child.component.html是:

@Routes([
  { path: '/', component: LoginComponent },
])   
<a [routerLink]="['/Login']"></a>
<router-outlet></router-outlet>
@Routes([
    { path: '/', component: DashboardComponent},
])
<li class="treeview">
    <a [routerLink]="['']">Dashboard</a>
</li>
<router-outlet></router-outlet>
  • 仪表板
  • 此方法同时适用于父级和子级,但我希望路由具有另一个路径,例如默认路径“/dashboard”。
    有没有办法在子组件中定义默认路由。

    将此选项放在路由配置中

    { ...,  terminal: true}
    

    我想你必须试试这个:-

    export const HomeRoutes: RouterConfig = [
        { path: '', redirectTo: '/home', terminal: true},
        { path: 'home', component: HomeComponent, terminal: true }
    ];
    
    PS:-未测试

    更新 您还可以尝试两次定义路线,如下所示:-

    { path: '/', component: HomeComponent}
    { path: 'home', component: HomeComponent}
    
    这样,同一组件就有了默认路由器和命名路由器


    希望能对您有所帮助。

    不,我想它会告诉路由器,如果这条路由匹配,它不需要检查其他匹配的路由。我更新了答案<代码>终端:对于
    主页
    路线,不需要为true。感谢您的推荐:)路由中的路径在新的alpha.7路由器中不应该有前导的
    /