question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ionic router. OnInit working only once

See original GitHub issue

Bug Report

Ionic version: [x] 4.1.0

Current behavior: Ionic router run OnInit only first time

Expected behavior: Run OnInit each time

I have an app with Angular. It is a simple list of items, after clicking on an item opened item details page. Two routes:

  • IndexComponent. Items list.
  • ItemDetailsComponent. Edit item page
const routes: Routes = [
  {
    path: 'index',
    component: IndexComponent
  },
  {
    path: 'item/:id',
    component: ItemDetailsComponent
  }
];

I use ItemsService -> get/save items.

export class IndexComponent implements OnInit {
  public items: Array<Item>;

  constructor(private itemsService: ItemsService) {
  }

  ngOnInit() {
    this.itemsService.getItems()
    .subscribe(data => this.items = data)
  }
}
export class ItemDetailsComponent implements OnInit {
  public item: Item;

  constructor(private route: ActivatedRoute,
              private itemsService: ItemsService) {
  }

  ngOnInit() {
    const id = this.route.snapshot.params['id'];
    this.itemsService.getItem(id)
    .subscribe(data => this.item = data)
  }
}

In Angular application, OnInit lifecycle event working each time when I open these pages. And it is fine. I should edit an item on ItemDetailsComponent, and after clicking back to IndexComponent I see new information.

I start an Ionic project. Changed router-outlet to ion-router-outlet Same structure, same routing. And now:

  • IndexComponent OnInit working the only first time. When I back to this page from ItemDetailsComponent - OnInit not rerun. Ionic reuse IndexComponent, But I need a new instance.

  • ItemDetailsComponent OnInit working the only first time for each item in the items list. But if I edit an item, return back to IndexComponent, update items handly(click a button, because OnInit not work). And open ItemDetailsComponent again - I see old information because OnInit not firing - this is an old instance.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:44 (5 by maintainers)

github_iconTop GitHub Comments

16reactions
kamvircommented, Sep 15, 2019

I used ionViewWillEnter() instead of ngOnInit(). It works,

8reactions
liamdebeasicommented, Mar 26, 2019

Hi @traziusbiztest,

Thanks for the issue! I can confirm this is a bug. By default, we cache the component when navigating (whereas a regular Angular component destroys it and re-creates it when going back). We have a fix in the works that should resolve issues where routing isn’t working after the first time, params aren’t being updated, etc.

Work on the fix is well underway, and I hope to have more info to share soon!

Thanks for using Ionic!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ionic 6 Oninit works only once - Stack Overflow
Ionic router run OnInit only first time. I have an Ionic application. But OnInit only works once, so there is an alternative.
Read more >
Angular Navigation: How Routing & Redirects Work in Angular ...
This guide covers how routing works in an app built with Ionic and Angular. The Angular Router is one of the most important...
Read more >
Ionic 4 and the Lifecycle Hooks in Angular | by Paul Stelzer
Are these lifecycles still working? ... Yes, Ionic has still this lifecycles (see in Github). Only ionViewDidLoad has been replaced with Angular Lifecycle...
Read more >
Ionic Navigation and Routing - Javatpoint
It can simply work as a stack of pages, where one page id on top of another page. As we know, the stack...
Read more >
3 Common Rxjs Pitfalls (and how to avoid them)
The router does not assume that the Observable will emit only one value, and it will wait for the Observable to to either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found