I am using 6.1.3 angular HTTP fetching every time I navigate to a component using routing. I am new to angular.
Can anyone please help.
I am fetching data in ngOnninit
I think it is the problem
import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
constructor(
private httpDataGetter: HttpClient
) { }
ngOnInit() {
this.httpDataGetter.get('https://my-json-server.typicode.com/alifrontend499/userdatafake/profile').subscribe(responsive => {
$(responsive).each((count, obj) => {
this.userdata.push(obj);
});
}, err => {
console.log(err);
});
}
userdata = []
}
i want the values to be fetched when i navigate to the link in my (case its home component) i used routing it worked and when fetching values it worked too but as i used this.http.get() in ngOninit when i first navigate to homecomponent it fetches values which is good but when i navigate to login and come back to homecomponent it fetches values again. i dont want it .is there any other way?