You do not want the page to refresh when viewed - Ionic 2+

When the user views the first screen for the first time, they are sent there via this.nav.setRoot (Page). This presents a problem when I have three other options for setting pages per page. For example, I go to the home page, which is configured as root, so the source data is loaded initially for the first time. Then the user goes to the message page. Then the user returns to the home page, the data is downloaded again. I do not want this to happen. I would only like to call it once, but due to setRoot it refreshes the page. Just like navCtrl.push (Page) and .pop, data is not updated. I have a hamburger navigation style, and so I have many roots for every page in hamburger navigation.

app.comp.ts

openPage() {
    this.nav.setRoot(Page);
  }
  openPageTwo() {
    this.nav.setRoot(MessagesPage);
  }

How to override nav.setRoot update? Or use something else?

thank

+4
source share
3 answers

By setting the root page using navCtrl.setRoot (), you tell Ionic that this page is the beginning of the navigation tree of your application. For example, after logging in you should .setRoot (HomePage);

If you want to switch from HomePage to MessagingPage, you must use navCtrl.push () to pop up MessagePage on top of HomePage. eg; navCtrl.push (MessagesPage);

+1
source

Exactly where are you updating your data? What function?

Life cycle events

I think your problem will be solved with the right lifecycle event . For example ionViewDidLoad:

. . , , . ionViewDidLoad - .

?

, . Root , , . , .

:

, , .

+2

, , , , .

some-provider.ts

import { Injectable } from '@angular/core';

@Injectable()
export class SomeProvider {

    public data: DataModel;

    constructor(public http: Http) {
    }
}

providers.

some-page.ts

@Component({
    selector: 'app-some-page',
    templateUrl: 'app-some-page.html'
})
export class AppSomePage {

    data: DataModel;

    constructor(public someProvider: SomeProvider) {
        this.data = this.someProvider.data;
    }
}
0
source

Source: https://habr.com/ru/post/1686427/


All Articles