简体   繁体   English

Angular 不需要的重新加载页面

[英]Angular unwanted reload page

When i click on a routerLink, it goes to the link page with full page reload (aka parent component refresh)当我单击 routerLink 时,它会转到链接页面并重新加载整页(又名父组件刷新)

I tried making a 2nd app-routing.module like in this example: https://angular.io/guide/router-tutorial-toh But it didnt reslove the problem.我尝试制作第二个 app-routing.module,如下例所示: https://angular.io/guide/router-tutorial-toh但它并没有解决问题。 Using router-outlet names neither.既不使用路由器插座名称。 Im running out of idees我没有想法了

My routes are like:我的路线是这样的:

const routes: Routes = [
  {
    path: 'account',
    component: StandardLayoutComponent,
    canActivate: [AuthGuard],
    children: [
      { path: 'schools', component: SchoolsComponent },
      {
        path: 'contacts',
        component: ContactsComponent,
      },
      {
        path: 'dashboard',
        component: DashboardComponent,
      },
    ],
  },
  {
    path: ':schoolSlug',
    component: RegistrationLayoutComponent,
    children: [
      {
        path: 'login',
        component: LoginComponent,
      },
      {
        path: 'forgot_password',
        component: ForgotPasswordComponent,
      }
    ],
  },
  { path: '**', redirectTo: 'account/dashboard', pathMatch: 'full' },
];

The sidenav comonent html:侧导航组件 html:

<div
  class="d-flex flex-row mb-3 pointer lightHover"
  [routerLink]="['dashboard']"
>
  <div class="align-self-center col-md-3 pr-0 text-center">
    <img src="/assets/img/icons/squares.svg" />
  </div>
  <div class="col-md-9">
    <h6 class="font-weight-bold text-gray-dark mb-1 pt-3">
      {{ "MENU.ANALYSE.TITLE" | translate }}
    </h6>
    <p class="sidenav-txt pb-3">
      {{ "MENU.ANALYSE.DESCRIPTION" | translate }}
    </p>
  </div>
</div>
<div
  class="d-flex flex-row mb-3 pointer lightHover"
  [routerLink]="['contacts']"
>
  <div class="align-self-center col-md-3 display-5 pr-0 text-center">
    <img src="/assets/img/icons/user.svg" />
  </div>
  <div class="col-md-9">
    <h6 class="font-weight-bold text-gray-dark mb-1 pt-3">
      {{ "MENU.CONTACTS.TITLE" | translate }}
    </h6>
    <p class="sidenav-txt pb-3">
      {{ "MENU.CONTACTS.DESCRIPTION" | translate }}
    </p>
  </div>
</div>

I have one router-outlet in app.component and one in standardLayoutComponent (in which i also call header and sidenav) For example when i click on routerlink from '/account/dashboard' to '/account/contacts', the standardLayoutComponent refresh (with header and sidenav) instead of only refreshing DashboardComponents我在 app.component 中有一个路由器插座,在 standardLayoutComponent 中有一个(我在其中也调用 header 和 sidenav)例如,当我单击从“/account/dashboard”到“/account/contacts”的路由器链接时,standardLayoutComponent 刷新(使用 header 和 sidenav) 而不是仅刷新 DashboardComponents

Thanks for the help (using latest version of angular)感谢您的帮助(使用最新版本的角度)

Ok i finaly resolved this.好的,我终于解决了这个问题。 I went through dumping my components to see if the problem was from them.我通过倾倒我的组件来查看问题是否来自它们。
And it was this line of code:就是这行代码:

constructor (
   ...
){
 this.router.routeReuseStrategy.shouldReuseRoute = function () {

      return false;

    };
}

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

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