I've encountered a weird thing while working with Angular dynamic routing. Dynamic routes are only working when called by a RouterLink
. But whenever I try to access the route by entering the URL directly into the browser the app crashes.
(Dev console logs Failed to load resource: the server responded with a status of 404 (Not Found)
for every required script file)
Is there something that I'm missing, or is there a specific reason why the dynamic routes only work when called by a RouterLink
?
For anyone interested, here are some pieces of code:
const appRoutes: Routes = [
{ path: "user", component: UserComponent },
{ path: "user/:userID", component: UserDetailComponent }
];
HTML
<ul class="list-unstyled">
<li *ngFor="let user of users">
<a [routerLink]="['/user', user]">{{ user }}</a>
</li>
</ul>
Really sorry if this is a moronic issue, but I've been struggeling with this for days now. Thanks!
Use HashLocationStrategy
. By Replacing RouterModule.forRoot(routes)
with RouterModule.forRoot(routes, {useHash: true})
.
Refer : https://codecraft.tv/courses/angular/routing/routing-strategies/ for more details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.