简体   繁体   English

Angular-即使没有href标记,routerLink仍会重新加载

[英]Angular - routerLink still reloading even if no href tag exists

I have this code: 我有以下代码:

<a [routerLink]="['/user/', user.id]">
    {{ user.username }}
</a>

and this is routing.ts 这是routing.ts

// imports removed for clarity

const routes: Routes = [  
  { path: '', component: MainLayoutComponent, children: [
      { path: '', component: SidebarLayoutComponent, children: [
          { path: 'feed', component: HomefeedComponent },
          { path: 'discover', component:DiscoverComponent },
        ]
      },
      { path: 'user/:id', component: UserComponent },

    ]}
];

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

and I have two pages. 我有两页。 Page 1 and Page 2. in both pages, one component is rendered: user-box-component.html, which has the above code. 页面1和页面2。在两个页面中,都呈现了一个组件:user-box-component.html,具有上面的代码。

but in Page 1, if I click on "username", it still reloads the page. 但在第1页中,如果我单击“用户名”,它仍会重新加载该页面。 in Page 2, it reloads only DOM, as expected. 在第2页中,它仅按预期重新加载DOM。

any idea, why in Page 1, still hard reload is happening? 任何想法,为什么在第1页中仍在进行硬重装?

EDIT: 编辑:

more infos: If I come to user page, and there the routerLink works well. 更多信息:如果我进入用户页面,则routerLink可以正常工作。 But if I go to /discover page, there it makes page reload. 但是,如果我转到/ discover页面,则会重新加载页面。 can it be that it makes reload because there is no routerState root? 可以因为没有routerState根而重新加载吗?

Check this stackblitz link . 检查此stackblitz链接。 I created sample app. 我创建了示例应用程序。

https://stackblitz.com/edit/routerlink-xqqztn https://stackblitz.com/edit/routerlink-xqqztn

Although it may not be exact your case scenario but i tried to do it as per your description 虽然可能不是您的情况确切,但我尝试按照您的描述进行操作

if this not solve issue please create stackblitz link 如果这不能解决问题,请创建stackblitz链接

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

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