在 Angular 单页面应用程序中配置默认路由和重定向
我正在尝试在 Angular 中创建一个单页应用程序,其中默认路由设置为 DOMAIN/main
。我的目标是将所有其他 URL 重定向到此默认路由,但 DOMAIN/privacy 除外,它应该打开与隐私相关的不同组件。
期望行为和实际行为的示例:
URL | 预期行为 | 实际行为 |
---|---|---|
DOMAIN | DOMAIN/main | DOMAIN/main |
DOMAIN/main | DOMAIN/main (只需打开 MainPageComponent) | DOMAIN/main/main |
DOMAIN /asdf/bcd | DOMAIN/main | DOMAIN/asdf/bcd (除了一些错误之外,没有任何反应,因为它找不到任何 runtime.js 以及其中的类似文件(显然)) |
DOMAIN/privacy | DOMAIN/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:
URL | Expected Behaviour | Actual Behaviour |
---|---|---|
DOMAIN | DOMAIN/main | DOMAIN/main |
DOMAIN/main | DOMAIN/main (just open MainPageComponent) | DOMAIN/main/main |
DOMAIN/asdf/bcd | DOMAIN/main | DOMAIN/asdf/bcd (nothing happens except for some errors because it cannot find any runtime.js and similar files in there (obviously)) |
DOMAIN/privacy | DOMAIN/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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除 main 上的
pathMatch: 'full'
并将其保存在隐私中。另外,你不需要 '**'它应该看起来像:
Remove
pathMatch: 'full'
on main and save it on privacy. Also, you no need '**'it should look like:
你可以像下面这样使用它。您可以提供组件本身,而不是重定向。另外,从“”以外的配置中删除 pathMatch 参数。
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 "".
天哪,我终于能够解决这个问题了。正如我已经怀疑的那样,路由本身并不是问题。为了构建 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": ""
toanglar.json
So it now works by simpliy removing
"baseHref": ""
or changing ig to"baseHref": "/"
.