How do I run a function on ngOnInit() only once and not again upon returning back from another...

Discussão em 'Angular' iniciado por anonym, Dezembro 5, 2024.

  1. anonym

    anonym Guest

    HomeComponent

    ngOnInit()
    {
    console.log('loaded');
    this.retrieveData();
    }

    retrieveData()
    {
    // this.dataService.getData().subscribe(...);
    }


    I am retrieving data when the component loads. When the user clicks on another routerLink, for example, SettingsComponent and returns back to the HomeComponent, the function of course, is called again because the component has loaded again. But Everytime I return back to the component, it makes the function call again which creates too many unwanted HTTP requests. I need to prevent this and make sure the function is called only the first time. How do I do this? Should I use some other component lifecycle hook?

    Continue reading...

Compartilhe esta Página