【问题标题】:Angular 2 default route when base href not root基本href不是root时的Angular 2默认路由
【发布时间】:2018-06-26 20:26:26
【问题描述】:

我已经定义了这些路线:

const routes: Routes = [
    { path: 'Home', component: DashboardComponent },
    { path: '', redirectTo: '/Home', pathMatch: 'full' }, 
    { path: '**', component: NotFoundComponent }, 
];

基本 href 设置如下:

<base href="/Site1/" />

当我导航到 localhost/Site1 时,我得到 NotFound 组件,而我希望它重定向到 /Home

如果我尝试 localhost/Site1/(带有尾部正斜杠),它会匹配默认路由并重定向到 /Home

如何让第一个 URL 正确重定向?

【问题讨论】:

  • 像这样在 Site1 之后删除斜线
  • 我厌倦了,但它没有帮助,同样的问题发生
  • 你不使用任何端口吗?比如3000还是4200?你实现了 HashLocationStrategy 了吗?
  • 也有完全相同的问题,你有没有找到任何原因为什么它会这样工作?

标签: angular angular2-routing


【解决方案1】:

您在路由定义中添加额外的/

    const routes: Routes = [
            { path: 'Home', component: DashboardComponent },
            ///////////////////////////////////////////////////////////////////////
            //        removed the extra slash in the below line 
            ///////////////////////////////////////////////////////////////////////
            { path: '', redirectTo: 'Home', pathMatch: 'full' },  
            { path: '**', component: NotFoundComponent }, 
        ];

【讨论】:

  • 不,这也没有帮助
  • 和之前一样,是路由到NotFound组件——**路由
  • 您可以在团队查看器中使用吗?
【解决方案2】:

更多的是解决方法而不是修复我通过替换使其工作:

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

与:

{ path: '', component: DashboardComponent },

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2020-01-15
    相关资源
    最近更新 更多