在 Angular 单页面应用程序中配置默认​​路由和重定向

发布于 2025-01-14 12:25:54 字数 1753 浏览 3 评论 0原文

我正在尝试在 Angular 中创建一个单页应用程序,其中默认路由设置为 DOMAIN/main。我的目标是将所有其他 URL 重定向到此默认路由,但 DOMAIN/privacy 除外,它应该打开与隐私相关的不同组件。

期望行为和实际行为的示例:

URL预期行为实际行为
DOMAINDOMAIN/mainDOMAIN/main
DOMAIN/mainDOMAIN/main (只需打开 MainPageComponent)DOMAIN/main/main
DOMAIN /asdf/bcdDOMAIN/mainDOMAIN/asdf/bcd (除了一些错误之外,没有任何反应,因为它找不到任何 runtime.js以及其中的类似文件(显然))
DOMAIN/privacyDOMAIN/privacy (只需打开 ImprintComponent)DOMAIN/privacy/main

(不正确的行为以粗体标记。)

我有浏览了几个帖子和示例,但我仍在努力实现所需的行为。我不完全理解重定向和路径的工作原理,以及为什么 ** 通配符与 /asdf/bcd 不匹配并重定向到 /main< /代码>。

这是我迄今为止尝试过的代码:

const routes: Routes = [
  {
    path: "main",
    component: MainPageComponent,
    pathMatch: "full",
  },
  {
    path: "privacy",
    pathMatch: "full",
    component: ImprintComponent
  },
  {
    path: "",
    redirectTo: "/main",
    pathMatch: "full",
  },
  {
    path: "**",
    redirectTo: "/main",
  }
];

app.modules.ts内部:

imports: [
  RouterModule.forRoot(routes)
],

问题

如何使其正常工作?我真的不明白那些奇怪的重定向/路径,也不明白为什么 **ie 与 /asdf/bcd 不匹配,因此重定向到 / main...

如果您需要更多信息,我会尽快提供。谢谢! :)

I'm trying to create a single-page application in Angular, where the default route is set to DOMAIN/main. My goal is to redirect every other URL to this default route, except for DOMAIN/privacy, which should open a different component related to privacy.

Examples with desired and actual behavior:

URLExpected BehaviourActual Behaviour
DOMAINDOMAIN/mainDOMAIN/main
DOMAIN/mainDOMAIN/main (just open MainPageComponent)DOMAIN/main/main
DOMAIN/asdf/bcdDOMAIN/mainDOMAIN/asdf/bcd (nothing happens except for some errors because it cannot find any runtime.js and similar files in there (obviously))
DOMAIN/privacyDOMAIN/privacy (just open ImprintComponent)DOMAIN/privacy/main

(incorrect behavior is marked in bold.)

I have gone through several posts and examples, but I'm still struggling to achieve the desired behavior. I don't fully understand how the redirects and paths work, and why the ** wildcard doesn't match /asdf/bcd and redirect to /main.

Here's the code I have tried so far:

const routes: Routes = [
  {
    path: "main",
    component: MainPageComponent,
    pathMatch: "full",
  },
  {
    path: "privacy",
    pathMatch: "full",
    component: ImprintComponent
  },
  {
    path: "",
    redirectTo: "/main",
    pathMatch: "full",
  },
  {
    path: "**",
    redirectTo: "/main",
  }
];

Inside app.modules.ts:

imports: [
  RouterModule.forRoot(routes)
],

Questions

How do I get this working properly? I don't really understand those strange redirects / paths and also don't get why **i.e., doesnt match /asdf/bcd and consequently redirects to /main...

If you need further information, I will provide them as soon as possible. Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪意 2025-01-21 12:25:54

删除 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 {}

Remove pathMatch: 'full' on main and save it on privacy. Also, you no need '**'

it should look like:

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 {}
許願樹丅啲祈禱 2025-01-21 12:25:54

你可以像下面这样使用它。您可以提供组件本身,而不是重定向。另外,从“”以外的配置中删除 pathMatch 参数。

const routes: Routes = [{
  path: "main",
  component: MainPageComponent,
},
{
  path: "privacy",
  component: ImprintComponent
},
{
  path: "",
  component: MainPageComponent,
  pathMatch: "full",
},
{
  path: "**",
  component: MainPageComponent,
}] 

you can use it like this below. Instead of redirecting you can gan give the component itself. Also, remove the pathMatch param from configuration other than "".

const routes: Routes = [{
  path: "main",
  component: MainPageComponent,
},
{
  path: "privacy",
  component: ImprintComponent
},
{
  path: "",
  component: MainPageComponent,
  pathMatch: "full",
},
{
  path: "**",
  component: MainPageComponent,
}] 
高速公鹿 2025-01-21 12:25:54

天哪,我终于能够解决这个问题了。正如我已经怀疑的那样,路由本身并不是问题。为了构建 Angular-Project,以便我可以在实时服务器上使用它,我将 "baseHref": "" 添加到 anglar.json

所以它现在只需删除 "baseHref": "" 或将 ig 更改为 "baseHref": "/" 即可实现

Omg, I finally was able to solve this. As I have already suspected, the routing itself wasn't the problem. For building the Angular-Project, so that I can use it on a live sever, I added "baseHref": "" to anglar.json

So it now works by simpliy removing "baseHref": "" or changing ig to "baseHref": "/".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文