我正试图在Angular中创建一个单页应用程序,其中默认路由设置为DOMAIN/main
。我的目标是将所有其他URL重定向到这个默认路由,除了DOMAIN/privacy
,它应该打开一个与隐私相关的不同组件。
具有期望和实际行为的示例:
URL | 预期行为 | 实际行为|||||
---|---|---|---|---|---|---|
DOMAIN/main | 域/main(只需打开MainPageComponent( | 域/main/strong>|||||
DOMAIN/asdf/bcd | DOMAIN/main | DOMAIN/asdf/bcd(由于在其中找不到任何runtime.js 和类似文件,所以除了一些错误外,什么都没发生( | ||||
域/隐私 | 域/隐私(只需打开印记组件( |
删除main上的pathMatch: 'full'
并将其保存在隐私中。此外,您不需要"***">
它应该看起来像:
const routes: Routes = [{
path: "main",
component: MainPageComponent,
},
{
path: "privacy",
component: ImprintComponent,
pathMatch: "full"
},
{
path: "",
redirectTo: "/main",
pathMatch: "full",
}]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
您可以像下面这样使用它。你不需要重定向,而是可以赋予组件本身。此外,从配置中删除pathMatch参数,而不是"&";。
const routes: Routes = [{
path: "main",
component: MainPageComponent,
},
{
path: "privacy",
component: ImprintComponent
},
{
path: "",
component: MainPageComponent,
pathMatch: "full",
},
{
path: "**",
component: MainPageComponent,
}]
Omg,我终于解决了这个问题。正如我已经怀疑的那样,路由本身并不是问题所在。为了构建Angular项目,以便我可以在实时服务器上使用它,我在anglar.json
中添加了"baseHref": ""
因此,它现在通过简单地删除"baseHref": ""
或将ig更改为"baseHref": "/"
来工作。