【问题标题】:Angular2 Default Route ViewAngular2 默认路由视图
【发布时间】:2016-07-16 09:48:11
【问题描述】:

我在app.ts 中定义了我的所有路线

@RouteConfig([
    { path: '/dashboard/...', component: Dashboard, name: 'Dashboard', useAsDefault: true }
)

在仪表板上,我有一些相对于仪表板的嵌套路由。
dashboard.ts

@Component({
    template: `<h1>this is dashboard default text</h1>
               <router-outlet></router-outlet>`
});
@RouteConfig([
    { path: '/cmp1' , name: 'Cmp1' , component: cmp1}
    { path: '/cmp2' , name: 'Cmp2' , component: cmp2}
)

现在,如果我转到根 url(即“/”),它会将我重定向到 /dashboard,因为它是默认的(但还没有显示 cmp1/cmp2,因为我还没有点击仪表板上的链接尚未)。
但是,如果我尝试直接访问/dashboard,它会显示一个空白页面。


我希望在我直接访问/dashboard 时显示仪表板的视图(即从浏览器地址栏且没有子组件视图)。它应该表现得好像我正在访问根 URL /
我怎样才能做到这一点?

【问题讨论】:

    标签: angular angular2-routing angular2-template


    【解决方案1】:

    您可以使用您的now dashboard 作为actual default dashboard 的父级,如下图所示(与 Günter 的几乎相似,我也有这样的)

    app.ts

    @RouteConfig([
        { path: '/dashboard/...', component: Dashboard, name: 'Dashboard', useAsDefault: true }
    )
    

    dashboard.ts

    @Component({
        template: `<h1>this is dashboard text for all child routes
                        including default dashboard.
                   </h1>
                   <router-outlet></router-outlet>`
    });
    @RouteConfig([
        { path: '/' , name: 'DashDefault' , component: DashDefaultCmp, useAsDefault: true},
        { path: '/cmp1' , name: 'Cmp1' , component: cmp1},
        { path: '/cmp2' , name: 'Cmp2' , component: cmp2}
    )
    

    dashDefaultCmp.ts

    (如果你愿意,可以留空)

    @Component({
            template: `<h1>this is your default dashboard.
                       </h1>`
        });
    export class DashDefaultCmp{
    }
    

    希望这会有所帮助:)

    【讨论】:

    • 嗯我想到了,但为此创建另一个组件似乎有点过头了。总之谢谢
    【解决方案2】:

    在您的一个子路由上也设置useAsDefault,否则如果您只输入//dashboard,路由器将无法知道要显示的组件

    Plunker example

    更新

    使用路由到不显示任何内容的子组件。

    @Component({
      selector: 'empty',
      template: ``
    })
    export class Empty {
    }
    
    @Component({
      selector: 'cmp1',
      template: `
      <h2>Cmp1</h2>
    `
    })
    export class Cmp1 {
    }
    
    @Component({
      selector: 'cmp2',
      template: `
      <h2>Cmp2</h2>
    `
    })
    export class Cmp2 {
    }
    
    @Component({
      selector: 'dashboard',
      directives: [ROUTER_DIRECTIVES],
      template: `
      <h2>Dashboard</h2>
      <router-outlet></router-outlet>
    `
    })
    @RouteConfig([
        { path: '/overview' , name: 'Overview' , component: Empty}
        { path: '/cmp1' , name: 'Cmp1' , component: Cmp1}
        { path: '/cmp2' , name: 'Cmp2' , component: Cmp2}
    )
    export class Dashboard {
    }
    
    @Component({
      selector: 'my-app',
      directives: [ROUTER_DIRECTIVES],
      template: `
      <h2>Hello {{name}}</h2>
      <a href="#" [routerLink]="['Dashboard', 'Overview']">Dashboard</a>
      <a href="#" [routerLink]="['Dashboard', 'Cmp1']">Cmp1</a>
      <a href="#" [routerLink]="['Dashboard', 'Cmp2']">Cmp2</a>
      <router-outlet></router-outlet>
    `
    })
    @RouteConfig([
        { path: '/dashboard/...', component: Dashboard, name: 'Dashboard', useAsDefault: true },
    ])
    export class App {
    }
    

    Plunker example

    【讨论】:

    • 当我访问/dashboard 时,它应该会显示仪表板模板上的内容(不包括子组件)。如果我将子组件设置为默认值,那么当我访问 /dashboard 时它会自动将我重定向到该路由
    • 所以当您输入/dashboard 时您不想显示任何CmpX?那么你需要一个/dashboard 路由(没有/...)。
    • /dashboard 在哪里?在app.ts?我不能用相同的组件创建两条路线,可以吗?它向我显示了这个错误-> Error during instantiation of Router! (RouterOutlet -&gt; Router). ORIGINAL EXCEPTION: Child routes are not allowed for "/dashboard". Use "..." on the parent's route path.
    • 我更新了我的答案。不过可能有更好的解决方案。我自己对路由器还没有太多经验。
    猜你喜欢
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2016-10-02
    • 2021-12-18
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多