【问题标题】:ngOnInit not being called after router.navigate在 router.navigate 之后没有调用 ngOnInit
【发布时间】:2017-06-25 10:17:34
【问题描述】:

我的 Angular 应用程序在 router.navigation() 之后突然没有调用 ngOnInit(),这意味着我的组件无法正确加载。我认为这可能是由于我进行了一些更改,但还原更改并没有解决问题。

正常导航导致组件无法正确加载的示例;此页面由以下代码清单导航到: this.router.navigate(['/result', this.params.data._id]);:

重新加载页面,组件加载正确:

这是我的一些代码清单,

app.module.ts

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    AgGridModule.withComponents(
            [SampleResultButtonComponent]
        ),
    ChartsModule
  ],
  declarations: [
    AppComponent,

    LoginComponent,
    LogoutComponent,
    SignupComponent,

    FilesComponent,
    NavbarComponent,
    ProfileComponent,
    UploadComponent,
    SampleGridApplicationComponent,
    SampleResultButtonComponent,
    AssetGridApplicationComponent,
    ResultComponent,
    ResetPasswordComponent,
    AssetComponentDetailComponent
  ],
  bootstrap: [ AppComponent ],
  entryComponents: [AssetComponentDetailComponent]
})
export class AppModule {}

app-routing.module.ts

    @Injectable()
export class NoAuthGuard implements CanActivate {
  constructor(private router: Router) {}

  canActivate() {
    const activeUser = Kinvey.User.getActiveUser();
    if (activeUser) {
      return true;
    }

    // Navigate to the login page
    this.router.navigate(['/login']);
    return false;
  }
}

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private router: Router) {}

  canActivate() {
    const activeUser = Kinvey.User.getActiveUser();
    console.log("AuthGuard, CanActivate");
    if (!activeUser) {
      return true;
    }

    // Navigate to the main page
    this.router.navigate(['']);
    return false;
  }
}

const appRoutes: Routes = [
  {
    path: '',
    component: NavbarComponent,
    canActivate: [NoAuthGuard],
    children: [
      { path: '', component: SampleGridApplicationComponent },

      { path: 'files', component: FilesComponent },
      { path: 'upload', component: UploadComponent },

      { path: 'profile', component: ProfileComponent },

      { path: 'sampleitems', component: SampleGridApplicationComponent },
      { path: 'assetitems', component: AssetGridApplicationComponent },
      { path: 'result/:id', component: ResultComponent}

    ]
  },
  { path: 'login', component: LoginComponent, canActivate: [AuthGuard] },
  { path: 'logout', component: LogoutComponent },
  { path: 'signup', component: SignupComponent, canActivate: [AuthGuard] },
  { path: 'reset', component: ResetPasswordComponent, canActivate: [AuthGuard] }
];
@NgModule({
  imports: [
    RouterModule.forRoot(appRoutes, {useHash: true})
  ],
  exports: [
    RouterModule
  ],
  providers: [
    AuthGuard,
    NoAuthGuard
  ]
})
export class AppRoutingModule {}

编辑 经过更多挖掘后,我认为该问题与here 问题有关,但建议的修复无法解决此问题。

【问题讨论】:

  • 你能提供你的代码吗?
  • 更新了代码清单。
  • 您的屏幕截图显示正在加载的不同组件...第一个显示SampleGridApplicationComponent,第二个显示ResultComponent。这是你所期待的吗?
  • 这是问题所在,SampleGridApplicationComponent 是上一个路由的组件,ResultComponent 是当前路由的组​​件。使用 router.navigate 时未加载组件。
  • 用可能的原因更新问题。

标签: angular angular-cli angular-routing


【解决方案1】:

看来这个问题还在,即使在5.0发布之后,至少https://github.com/angular/angular/issues/18254这个问题还没有解决。好消息是问题中描述了一种解决方法,使用:

        this.zone.run(() => {
          this.router.navigateByUrl( url );
        });

我在 Firebase 的 onAuthStateChanged 回调中遇到了问题,上述解决方法有所帮助。

【讨论】:

  • 想详细说明错误的细节?为什么会这样?问题的本质是什么?哪些情况会受到影响?
  • 我在 5+ 版本也遇到了同样的问题。谢谢!我可以从issue link 找到解决方案
  • 从 Angular 5.2.0 升级到 Angular 6 后我遇到了同样的问题。谢谢
  • @angular/router@7.2.0 也看到了这个问题。按照此处的建议手动运行NgZone 解决了问题。在撰写本文时,我不明白为什么需要这样做。
【解决方案2】:

问题最终成为 Angular 路由器中的一个错误,降级到版本 4.1.3 修复了该问题。希望这可以帮助任何尝试使用最新版本的 Angular 和 Kinvey SDK 的人。

【讨论】:

  • 想详细说明错误的细节?为什么会这样?问题的性质是什么?哪些情况会受到影响?
猜你喜欢
  • 2017-11-03
  • 2020-01-10
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多