【问题标题】:Angular 8 Router Link not working in a specific part of the codeAngular 8 Router Link 在代码的特定部分不起作用
【发布时间】:2020-10-06 01:26:24
【问题描述】:

在我的代码中,如果用户未经过身份验证且工作正常,我使用 routerLink 重定向用户,但在同一页面中,我有几个链接重定向到建议的帖子,但在点击时,只有浏览器中的链接会更改,并且单击的帖子无法加载,这是我当前版本的代码:

注意:RouterModule 已经导入 app.module.ts

import { RouterModule } from '@angular/router';
@NgModule({
    declarations: [
      ...
      FaqComponent,

    ],
    imports: [
      RouterModule,
      BrowserModule,
      ...
    ],
    providers: [],
    bootstrap: []
  })
  export class AppModule { }

不工作:

<h1>Suggested FAQ</h1>
<ul class="list-group list-group-flush">
     <li style="padding: 12px 0px;background-color: transparent;font-weight: bold;" *ngFor="let faq of suggestedFaqs" class="list-group-item">
        <a routerLink="/faq/{{faq.id}}/{{faq.title}}" >{{faq.title}}</a>
     </li>
</ul>

作品:

<div style="margin: 30px 0px;" *ngIf="!isLoggedIn">
   <a routerLink="/login" class="btn_faq" >login</a>
</div>

app-routing.module.ts 中的路径定义

  { path: 'faq/:id/:title', component: FaqComponent},

例子:

http://localhost:4200/faq/2/Changer%20votre%20langue%20préférée.

当它在 FaqComponent 外部单击时,该路由可以正常工作,但如果在 FaqComponent 内部单击它则不起作用

【问题讨论】:

  • 你能显示你的控制台错误吗?
  • @StepUp 没有显示错误,它只是不加载新的帖子详细信息,但浏览器中的链接会更改
  • '/faq' 路由是否使用您在 app.routing.ts 文件中传递的参数定义?它应该显示为 { path: 'faq/:title', component: FaqComponent}
  • 是的,它已经定义并且相同的 routerLink : [routerLink]="['/faq', faq.id,faq.title]" 重定向到包含所有帖子的页面中的帖子跨度>
  • 您的路线看起来如何? .../faq/1/footitle你能显示你的路径吗?

标签: angular angular-routerlink


【解决方案1】:

路由器仅在导航时销毁并重新创建组件 到不同的路线。当只有路由参数或查询参数时 更新但路由相同,组件不会被销毁 并重新创建。

Answer here

【讨论】:

    猜你喜欢
    • 2017-05-19
    • 2021-08-18
    • 2020-07-28
    • 2019-12-06
    • 2020-06-03
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    相关资源
    最近更新 更多