当base href不是根目录时,Angular 2的默认路由是什么?

4

我有以下定义的路由:

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

并且基础链接应该设置为这样:

<base href="/Site1/" />

当我访问localhost/Site1时,我得到了NotFound组件,但我希望它重定向到/Home。
如果我尝试localhost/Site1/(带有尾随正斜杠),它会匹配默认路由并重定向到/Home。
如何使第一个URL正确重定向?

1
只需删除Site1后面的斜杠,像这样:<base href="/Site1" /> - digit
我尝试过了,但没有帮助,问题仍然存在。 - prb
你没有使用任何端口吗?比如3000或4200吗?并且你是否已经实现了HashLocationStrategy? - The Hungry Dictator
同样遇到了这个问题,你找到为什么会这样工作的原因了吗? - Maksym
2个回答

1
您在路由定义中添加了额外的/
    const routes: Routes = [
            { path: 'Home', component: DashboardComponent },
            ///////////////////////////////////////////////////////////////////////
            //        removed the extra slash in the below line 
            ///////////////////////////////////////////////////////////////////////
            { path: '', redirectTo: 'Home', pathMatch: 'full' },  
            { path: '**', component: NotFoundComponent }, 
        ];

不,这也没有帮助。 - prb
同之前一样,它正在路由到NotFound组件- **路由。 - prb
你在Team Viewer上可用吗? - Aravind

0

这更像是一个变通方法而不是一个修复方法,我通过替换“:”来使其正常工作。

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

使用:

{ path: '', component: DashboardComponent },

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接