Tengo una estructura de aplicación básica donde llamo a los datos (los iPhones en la base de datos) desde el propio AppComponent.
ngOnInit(): void { this.iphoneservice.fetchIphones(); }El método fetchIphones está en mi archivo iPhoneService donde tengo 3 funciones.
iPhoneList:iPhone[]=[] fetchIphones(){ this.http .get('https://angular-project-62585-default-rtdb.firebaseio.com/iphone.json') .pipe( map(responseData =>{ const postArray =[]; for(const key in responseData){ if(responseData.hasOwnProperty(key)){ postArray.push({...responseData[key],id:key}); } } return postArray }) ) .subscribe(post =>{ this.generateIphoneList(post) }) } // Generating the IphoneList from the fetched Data generateIphoneList(post){ this.iPhoneList=[]; for (let iphones of post){ let iphone = new iPhone() for (let property in iphones){ iphone[property]=iphones[property]; } this.iPhoneList.push(iphones); } } // Returning the IphoneList to desired components getiPhone(){ return this.iPhoneList; }Las dos primeras funciones crean la matriz iPhoneList y luego desde mi iPhoneFront-Component recupero los datos con la función getiPhone.
ngOnInit(): void { this.iPhoneList=this.iphoneservice.getiPhone(); }Mi problema es que cuando voy a la página a través de la barra de navegación, los datos se obtienen y son visibles, pero si voy a la página web manualmente a través de la URL, los datos no se obtienen. Mi HTML para la barra de navegación y AppModule están a continuación.
<li class="nav-item"> <a class="nav-link" routerLink="/iphone" routerLinkActive="active" [routerLinkActiveOptions]="{exact:true}">iPhone</a> </li> const appRoutes : Routes =[ {path:'iphone', children:[ {path:'',component:IphonefrontComponent}, {path:':id',component:IphonedetailComponent}, ] }, {path:'iphone-managing', canActivate:[AuthenticationGuard], children:[ {path:'',component:IphonebackComponent}, {path:'create',component:IphonecreateComponent}, {path:':id',component:IphoneeditComponent}, ]}, {path:'cart',component:CartComponent}, {path:'authentication',component:AuthenticationComponent}, ]Gracias de antemano :)