【问题标题】:Route to non-empty default route in angular 2以角度 2 路由到非空默认路由
【发布时间】:2016-06-02 06:09:47
【问题描述】:

使用新的 RC 路由器定义默认路由的一种方法是

@Routes([{ path: '/',  component: Home }])

但我如何才能在最初显示一个非空路径的页面?喜欢

@Routes([{ path: '/home',  component: Home }])

这永远不会显示主页内容,但我想要它。

如何在新的 RC1 路由器中做到这一点?

【问题讨论】:

    标签: angular angular2-routing angular2-router


    【解决方案1】:

    >= RC.4

    { path: '', redirectTo: '/home', pathMatch: 'full' }
    { path: '/home',  component: Home }
    

    useAsDefault 在新路由器中尚不支持。为转发到/home/ 路由创建一个虚拟组件

    export class DummyComponent {
      constructor(private router:Router) {
        setTimeout(() => this.router.navigate('/home'));
      }
    }
    

    目前也没有选项可以跳过将导航添加到历史记录,这可能会导致返回按钮出现问题。

    正在处理路由器问题...

    【讨论】:

    • 我认为setTimeout() 没有必要(经过最近的一些测试)。
    • 由于最新的路由器 rc1 戏剧,我停止使用 angular 2...不过谢谢。
    【解决方案2】:

    使用 rc4 你可以像这样使用通配符路由 { path: '**', redirectTo: 'link/to/default/route' }

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      相关资源
      最近更新 更多