Angular4:默认重定向不起作用

时间:2018-01-22 15:33:05

标签: angular

我有一个看起来像这样的路径文件。 我的app.routes.ts

// my imported files
 .
 .
 .
const routes: Routes = [
  {
    path: '',
    component: WebsiteComponent,
    children: [
      { path: "", redirectTo: "job", pathMatch: "full" },
      { path: 'profile/:user_id', component: ProfileComponent },
      {
        path: 'profile/:user_id/downloadResume',
        component: DownloadResumeComponent
      },
      {
        path: 'profile/:user_id/profileSettings',
        component: SettingsComponent
      },
      {
        path: 'detect-device/:user_id',
        component: SecureDeviceDetectionComponent
      },
      {
        path: 'network',
        loadChildren: 'app/website/my-network/my-network.module#MyNetworkModule'
      },
      { path: 'inbox', loadChildren: 'app/website/inbox/inbox.module#InboxModule' },
      { path: 'groups', loadChildren: 'app/website/group/group.module#GroupModule' },
      { path: "qa", loadChildren: "app/website/qa/qa.module#QaModule" },
      {
        path: "notification",
        loadChildren: "app/website/notification/notification.module#NotificationModule"
      },
      {
        path: 'feed',
        loadChildren: 'app/website/feed/feed.module#FeedModule'
      },
      { path: 'register', component: RegisterComponent },
      { path: 'company-registration', component: CompanyRegisterComponent },
      { path: 'login', component: LoginComponent },
      { path: 'two-way-authentication', component: TwoWayAuthenticationComponent },
      { path: 'resendCode', component: TwoWayAuthenticationComponent },
      {
        path: ':id/password',
        component: ResetPasswordComponent
      },
      { path: 'forget-password', component: ForgetPasswordComponent },
      { path: 'job', loadChildren: 'app/website/jobs/search/job-search.module#JobSearchModule' },
      // { path: 'jobs', loadChildren: 'app/website/jobs/jobs.module#JobsModule' },
      { path: 'unsubscribe', component: UnsubscribeComponent },
      { path: 'feedback', component: FeedbackComponent },
    ]
  },
  { path: 'admin', loadChildren: './admin/routes/routes.module#RoutesModule' },
  { path: 'employer', loadChildren: './employer/routes/routes.module#RoutesModule' },
  { path: '**', pathMatch: 'full', redirectTo: 'job' },
];

export const appRouting = RouterModule.forRoot(routes);

app.module.ts

  // imported files 
 .
 .
  .

// https://github.com/ocombe/ng2-translate/issues/218
export function createTranslateLoader(http: Http) {
  return new TranslateStaticLoader(http, "./assets/i18n", ".json");
}

const url = configuration.SOCKET_URL;

@NgModule({
  declarations: [
    AppComponent,
    UnsubscribeComponent,
    ProfileSettingComponent,
    WebsiteComponent
  ],
  imports: [
    BrowserModule,
    appRouting,
    UsersModule,
    FormsModule,
    HttpModule,
    Ng2PaginationModule,
    SocketModule.forRoot(url),
    SweetAlert2Module.forRoot(),
    AlertModule.forRoot(),
    CommonModule,
    GeneralUseModule,
    TranslateModule.forRoot({
      provide: TranslateLoader,
      useFactory: createTranslateLoader,
      deps: [Http],
    }),
    NgbModule.forRoot(),
    BrowserAnimationsModule,
    TagInputModule,
    NotificationModule,
    FeedbackModule,
  ],
  providers: [
    { provide: LocationStrategy, useClass: environment.production ? HashLocationStrategy : PathLocationStrategy }, 
    SubscribeService,
    ToastrService
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

当网址为http://localhost:4200时,它应该重定向到job路由,但它不会这样做。

我想知道为什么它不起作用。我不知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

最后,我想出了解决方案。我已经使用Augury调试了路由,我意识到我有两个相同的默认路由/

Picture

根据上图,NotificationListComponentWebsiteComponent/条路线。因此,默认路线将在首次匹配时保持NotificationListComponent

我已移除NotificationModule并将所需的组件从NotificatoinModule添加到AppModule。现在它完美无缺。

我回答了我的问题,因为它可能有助于其他人:)