【问题标题】:Angular appends to router-outlet instead of replacing contentAngular 附加到路由器出口而不是替换内容
【发布时间】:2021-01-31 12:38:27
【问题描述】:

正在学习 Angular,当我使用 routerLink 或 this.router.navigate 时,组件只是附加到 router-outlet 而不是替换内容。这是我的代码

应用路由模块

const routes: Routes = [
  {path: 'Billing', component: BillingComponent},
  {path: 'BillDisplay', component: BillDisplayComponent}
];

app.component.module.html

<a routerLink="/Billing">
    Go to Billing
</a>
<router-outlet>
</router-outlet>

当我单击呈现页面上的链接时,我得到的计费页面搞砸了 Jquery,但我尝试直接在浏览器中提供相同的 URL,http://localhost:4200/billing,页面显示正常.另外,我有一个结算按钮,

billing.component.html

<input type="button" (click)="navigateTo()" value="Submit">

billing.component.ts

navigateTo(): void {
    this.router.navigate(['/BillDisplay']);
  }

现在,当我单击按钮时,billdisplay 的内容会显示在附加到帐单的同一页面中,如下所示。

contents of app.component.html
contents of billing.component.html
contents of billdisplay.html

为什么导航没有替换内容?

【问题讨论】:

  • 能否提供图片?这将有助于更好地理解问题。
  • 请张贴账单显示 Html 和代码
  • 发布更多代码,不清楚你在问什么。你的组件的html和js以及你的路由

标签: javascript angular


【解决方案1】:

试试这个

 this.router.navigateByUrl('/', { skipLocationChange: true }).then(() =>{
          this.router.navigate(['/BillDisplay']);
        })

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 2018-01-19
    • 2019-07-03
    • 2017-09-09
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多