首页 文章

Angular 2:刷新时路由到默认值

提问于
浏览
2

我想知道是否有可能告诉Angular 2应用程序在页面刷新时重定向到默认路由( localhost:8080/ ) .

见例子:

localhost:8080/#/home    ---refresh action--->     localhost:8080/

2 回答

  • 2

    一种方法是关闭 initialNavigation ,导入路由器模块:

    RouterModule.forRoot(APP_ROUTES, {initialNavigation: false})
    

    然后在根服务的某个构造函数中或者您希望的任何地方注入路由器并导航到 / .

  • 0

    您可以使用此代码,它将始终重定向到“默认”路由

    history.pushState(null, null, '/');
    window.addEventListener('popstate', function(event) {
        history.pushState(null, null, '/');
    });
    

相关问题