简体   繁体   English

Angular 8:动态路由仅在使用 RouterLink 时才有效?

[英]Angular 8: Dynamic Routing only working when using RouterLink?

I've encountered a weird thing while working with Angular dynamic routing.我在使用 Angular 动态路由时遇到了一件奇怪的事情。 Dynamic routes are only working when called by a RouterLink .动态路由仅在被RouterLink调用RouterLink But whenever I try to access the route by entering the URL directly into the browser the app crashes.但是每当我尝试通过直接在浏览器中输入 URL 来访问路由时,应用程序就会崩溃。

(Dev console logs Failed to load resource: the server responded with a status of 404 (Not Found) for every required script file) (开发控制台日志Failed to load resource: the server responded with a status of 404 (Not Found)每个所需脚本文件Failed to load resource: the server responded with a status of 404 (Not Found)

Is there something that I'm missing, or is there a specific reason why the dynamic routes only work when called by a RouterLink ?是否有我遗漏的东西,或者动态路由仅在被RouterLink调用时才起作用的特定原因?

For anyone interested, here are some pieces of code:对于任何感兴趣的人,这里有一些代码:

const appRoutes: Routes = [
   { path: "user", component: UserComponent },
   { path: "user/:userID", component: UserDetailComponent }
];

HTML 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 .使用HashLocationStrategy By Replacing RouterModule.forRoot(routes) with RouterModule.forRoot(routes, {useHash: true}) .通过用RouterModule.forRoot(routes, {useHash: true})替换RouterModule.forRoot(routes) RouterModule.forRoot(routes, {useHash: true})

Refer : https://codecraft.tv/courses/angular/routing/routing-strategies/ for more details.有关更多详细信息,请参阅: https : //codecraft.tv/courses/angular/routing/routing-strategies/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM