Angular 2路线更改为相同组件导致重新加载 [英] Angular 2 route change to same component causing reload

查看:75
本文介绍了Angular 2路线更改为相同组件导致重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找有关为何更改到Angular 2应用程序中相同组件的路由的一些建议.

Looking for some advice on why a route change to the same component in my Angular 2 application is reloading the component.

我有2条路线,都具有相同的组成部分:

I have 2 routes, both with the same component:

  • /home
  • /home/:id
const appRoutes = [
    {path:'', redirectTo:'/home', pathMatch:'full'},
    {path:'home', component: HomeComponent},
    {path:'home/:id', component: HomeComponent},
];

在两条路径之间切换时,将重新加载组件.在第二条路径上更改参数时,未重新加载组件(按预期方式).

When changing between the two routes, the component is reloaded. When changing the parameter on the second route, the component is not reloaded (as expected).

是否有任何方法可以像更改参数一样在不重新加载组件的情况下在这些路由之间进行更改?

Is there any way of being able to change between these routes without reloading the component, just as changing the parameter does?

查看此柱塞以了解我的意思

推荐答案

我遇到了同样的问题,因此这是我的解决方案.希望对您有所帮助.

I have the same issue so here is my solution. Hope it helps.

    {
      path: '',
      redirectTo: 'home/',
      pathMatch: 'full',
    },
    {
      path: 'home',
      redirectTo: 'home/',
      pathMatch: 'full',
    },
    {
      path: 'home/:id',
      component: HomeComponent,
    }

这篇关于Angular 2路线更改为相同组件导致重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆