简体   繁体   English

RouterLink 不会在单击时导航到组件,而是在加载同一模块中的其他组件后导航

[英]RouterLink does not navigate to a component on click but navigates after loading other component in same module

In my Angular 8 application I have following routes.在我的 Angular 8 应用程序中,我有以下路线。

Public routing module:公共路由模块:

const routes: Routes = [
  { path: '', redirectTo: 'login', pathMatch: 'full' },
  { path: 'login', component: LoginComponent },
  { path: 'forgot-password', component: ForgotPasswordComponent },
  { path: 'reset-password', component: ResetPasswordComponent }
];

User routing module:用户路由模块:

const routes: Routes = [
  { path: 'profile', component: UserProfileComponent },
  { path: 'purchase-agreement', component: PurchaseAgreementComponent },
  { path: 'purchase-credit', component: PurchaseCreditComponent },
  { path: 'purchase-confirmation', component: PurchaseConfirmationComponent },
  { path: 'purchase-thankyou', component: PurchaseThankyouComponent },
  { path: 'expenditure-report', component: ExpenditureReportComponent },
  { path: 'subscriptions', component: SubscriptionComponent },
  { path: 'subscribe', component: SubscribeComponent }
];

App routing module:应用路由模块:

const routes: Routes = [

  {
    path: 'startup',
    component: StartupComponent,
    canActivate: [AuthenticationGuardService]
  },
  {
    path: 'account',
    loadChildren: () => import('./modules/core-modules/user/user.module').then(m => m.UserModule),
    canActivate: [AuthenticationGuardService]
  },
  {
    path: '**',
    redirectTo: 'startup',
    pathMatch: 'full'
  }
];

Header Html where I have put routerLinks to navigate to account route and it's children: Header Html 我在其中放置了 routerLinks 以导航到account路由及其子节点:

    <mat-menu #accountMenu [overlapTrigger]="false" yPosition="below">
        <button mat-menu-item [routerLink]="['/account/profile']">
            <mat-icon>person</mat-icon><span>My Profile</span>
        </button>
        <button mat-menu-item [routerLink]="['/account/purchase-agreement']">
            <mat-icon>credit_card</mat-icon><span>Purchase Credits</span>
        </button>
        <button mat-menu-item [routerLink]="['/account/expenditure-report']">
            <mat-icon>timeline</mat-icon><span>Expenditure Report</span>
        </button>
        <button mat-menu-item [routerLink]="['/account/subscriptions']">
            <mat-icon>card_membership</mat-icon><span>Subscriptions</span>
        </button>
        <mat-divider></mat-divider>
        <button mat-menu-item (click)="logout()">
            <mat-icon>exit_to_app</mat-icon>Logout
        </button>
    </mat-menu>

App Module Code:应用模块代码:

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

// Helper Classes
import { JwtInterceptor, ErrorInterceptor, LoaderInterceptor } from 'app/_helpers';

// Feature Modules.
import { PublicModule } from 'app/modules/core-modules';

// Shared Functionality Modules.
import { MaterialElementsModule, SharedModule } from 'app/modules/generic-modules';

import { AppRoutingModule, mainRoutedComponents } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent, FooterComponent } from 'app/components';


@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    mainRoutedComponents
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    SharedModule,
    MaterialElementsModule,
    PublicModule,
    AppRoutingModule
  ],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: LoaderInterceptor, multi: true }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Problem:问题:

After login, I am taken to startup component, where header component is visible.登录后,我被带到启动组件,其中 header 组件可见。 And I click on my profile link and it does not load .然后我点击我的个人资料链接,它没有加载 But when I click on any other route/ link below it, I am being able to navigate to those components.After navigating to working components again I click on my profile from header.但是当我点击它下面的任何其他路线/链接时,我可以导航到这些组件。再次导航到工作组件后,我从 header 中单击我的配置文件。 This time I am being take to profile component.这次我被带到配置文件组件。

This is the console error I am getting when I try to navigate to my profile component after login.这是我在登录后尝试导航到我的profile组件时遇到的控制台错误。

控制台错误

Kindly provide some insight about this error.请提供有关此错误的一些见解。 How is it can be solved?如何解决?

I think, after login when you are trying to navigate to profile for very first time than Account Module is not loaded that is why URL Path is not completed and you redirect to the startup due to this route我认为,登录后,当您第一次尝试导航到配置文件时,帐户模块未加载,这就是 URL 路径未完成并且由于此路线而重定向到启动的原因

{
    path: '**',
    redirectTo: 'startup',
    pathMatch: 'full'   }

You have to make sure that your account module is loaded first您必须确保首先加载您的帐户模块

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

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