【问题标题】:Angular doesnt destroy source component after routingAngular 在路由后不会破坏源组件
【发布时间】:2020-12-07 15:04:32
【问题描述】:

我正在构建一个 Angular 8 应用程序,它有 2 个组件:LandingComponent、BlogDetailComponent,当然还有主要组件 AppComponent

如下所示,有从 LandingComponent 到 BlogDetailComponent 的路由。当我单击博客详细信息锚时,它成功地将页面重定向到 BlogDetailComponent 但问题是定向页面还包含登录页面信息。像这样;

Landing Page

Blog Detail Content

我很困惑,我希望源页面被销毁并创建目标页面。但我想我错了。有什么帮助吗?

app-component.html

<html>
<app-landing></app-landing>
</html>

登陆组件.html

    ...
<li><a routerLink="blog-detail">Landing Page</a></li>
<router-outlet></router-outlet>
   ...

blog-detail-component.html

    ...
   <div>Blog Detail Content</>
   ...

app-routing-module.ts

    ...
    const routes: Routes = [
        { path: 'blog-detail', component: BlogDetailComponent }
    ];

    @NgModule({
       declarations: [],
       imports: [CommonModule, RouterModule.forRoot(routes)],
       exports: [RouterModule]
    })
    export class AppRoutingModule { }

【问题讨论】:

  • 放在app.component.html中而不是放在landing-component.html中,新建路由{ path: '', component: LandingComponent } 默认加载登陆组件
  • 它工作了@Abhijit 我还从 app-component.html 中删除了 以便所有方向都基于路线文件。谢谢你。如果您添加为答案。我会批准的。
  • 你修好了它,添加为答案????

标签: angular angular-ui-router angular8


【解决方案1】:

&lt;router-outlet&gt;&lt;/router-outlet&gt; 放在app.component.html 中而不是landing-component.html 中,并创建一个新路由{ path: '', component: LandingComponent } 默认加载登陆组件

【讨论】:

    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2017-04-28
    • 2018-07-11
    • 2017-06-12
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多