Issue
I want to create a single page application with the default-route DOMAIN/main
. My goal is, that every other URL gets redirected to exactly this one. The only exception is DOMAIN/privacy
, which should open another component with privacy-related stuff.
Examples
Examples with desired and actual behaviour:
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 |
(Wrong ones are marked bold)
My Best Try so far
My best try was the following (which lead to the above behaviour):
I read through many posts but for me nothing really worked like I would expect it to do (not even the **
part...).
const routes: Routes = [{
path: "main",
component: MainPageComponent,
pathMatch: "full",
},
{
path: "privacy",
pathMatch: "full",
component: ImprintComponent
},
{
path: "",
redirectTo: "/main",
pathMatch: "full",
},
{
path: "**",
redirectTo: "/main",
}]
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! :)
Solution
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": "/"
.
Answered By - Niklas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.