[英]constructor or ngoninit is not calling when used router.navigate in ionic 4 with angular
i am using ionic 4 with angular i am navigating from home page to login when login status is false 我使用带有角度的ionic 4,当登录状态为false时,我正在从主页导航到登录
this.router.navigateByUrl('/login');
and also from login page to home page after login 以及登录后从登录页面到主页
this.router.navigateByUrl('/home');
my router modile is 我的路由器版本是
import { NgModule } from '@angular/core'; 从'@ angular / core'导入{NgModule}; import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
从'@ angular / router'导入{PreloadAllModules,RouterModule,Routes};
const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
path: 'home',
loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
},
{
path: 'list',
loadChildren: () => import('./list/list.module').then(m => m.ListPageModule)
},
{ path: 'acceptordermodalpage', loadChildren: './acceptordermodalpage/acceptordermodalpage.module#AcceptordermodalpagePageModule'},
{ path: 'newordermodalpage', loadChildren: './newordermodalpage/newordermodalpage.module#NewordermodalpagePageModule' },
{ path: 'rejectordermodalpage', loadChildren: './rejectordermodalpage/rejectordermodalpage.module#RejectordermodalpagePageModule' },
{ path: 'orderdetailsmodalpage', loadChildren: './orderdetailsmodalpage/orderdetailsmodalpage.module#OrderdetailsmodalpagePageModule' },
{ path: 'login', loadChildren: './login/login.module#LoginPageModule' }
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
now the problem is when i navigate from one page/component to another page/component the ngoninit or the constructor of destination page/component doesn't fire 现在的问题是,当我从一个页面/组件导航到另一页面/组件时,ngoninit或目标页面/组件的构造函数不会触发
but when i navigate using routerlink it works 但是当我使用routerlink导航时
[routerLink]="[p.url]"
i found some suggestion in online searching and i applied them 我在在线搜索中发现了一些建议,并应用了它们
in router module in changed to 在路由器模块中更改为
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules, onSameUrlNavigation: 'reload' })
],
i put following into both source and destination page/component constructor/ngoninit 我将以下内容放入源页面和目标页面/组件构造器/ ngoninit中
this.router.routeReuseStrategy.shouldReuseRoute = () => {
return false;
};
also i applied NgZone 我也应用了NgZone
this.ngZone.run(() => {
this.router.navigateByUrl('/login');
});
but nothing seems work to fire ngoninit or constructor after using router.navigateByUrl() 但是在使用router.navigateByUrl()之后似乎没有任何工作可以激发ngoninit或构造函数
can anyone please help. 谁能帮忙。
I think you need to use ionViewDidLoad. 我认为您需要使用ionViewDidLoad。 use like below
使用如下
ionViewDidLoad() {
// your ngoninit function's code
}
For details check Ionic navigation lifecycle events. 有关详细信息,请检查离子导航生命周期事件。 https://ionicframework.com/blog/navigating-lifecycle-events/
https://ionicframework.com/blog/navigating-lifecycle-events/
We can invoke ngOnInit or Constructor every time by using this.navCtrl.navigateBack(Ionic's) . 我们可以每次使用this.navCtrl.navigateBack(Ionic's)调用ngOnInit或Constructor 。 If we don't use(navigateBack) we can't invoke ngOnInit or Constructor.
如果不使用(navigateBack),则无法调用ngOnInit或Constructor。
(why ngOnInit doesn't invoked every time) :: when a route enters in stack in ionic framework( it stores routes inside stack )doesn't reintialize from starting (为什么ngOnInit不会每次都被调用)::当路径在ionic框架中进入堆栈( 它将路径存储在堆栈中 )不会从开始重新初始化
navCtrl.navigateBack it will make all existing pages in the stack will be removed, and the navigated page will become the single page in the stack. navCtrl.navigateBack它将使堆栈中所有现有的页面都将被删除,并且导航的页面将成为堆栈中的单个页面。
invoking ngOnInit & constructor stackblitz 调用ngOnInit和构造函数stackblitz
invoking ngOnInit & constructor github 调用ngOnInit和构造函数github
Note: check console.logs in above mentioned urls. 注意:在上述URL中检查console.logs。 (check .ts files inside 'home' & 'list' folders)
(检查“主”和“列表”文件夹中的.ts文件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.