0

I've been working on getting a resolver working on for an activated route. The problem I'm running into is that in my route's component the data I'm trying to get from the resolver is always undefined. I know my service works (I've returned values to the console) it just when I try and subscribe to my routes data in the component that nothing is returned

Here is my component

 @Component({
 selector: 'app-value',
 templateUrl: './value.component.html',
 styleUrls: ['./value.component.css']
})
export class ValueComponent implements OnInit , OnDestroy {

values: any = {};
newBin: any = [];
navigationSubscription;
bins: Bins;

constructor(private http: Http, private dbService: DbService,           private toastr: ToastrService, private router: Router,
private route: ActivatedRoute) {
this.navigationSubscription = this.router.events.subscribe((e: any) => {
 if (e instanceof NavigationEnd) {
    this.loadLinks();
  }
});
}

ngOnInit() {
 this.dbService.getBins().subscribe( response => {
  this.values = response;
 }
);
 this.loadLinks();
}

loadLinks() {
this.route.data.subscribe(data => {
  this.bins = data['bins'];
  console.log(this.bins);
 });
}

here is my app.module with the route

@NgModule({
  declarations: [
  AppComponent,
  ValueComponent,
  ],
  imports: [
  BrowserModule,
  HttpModule,
  RouterModule.forRoot(
  [{path: 'bins', component: ValueComponent, resolve: {bins: LinkResolver}, runGuardsAndResolvers: 'always'},
  {path: '', redirectTo: 'bins', pathMatch: 'full' }],
  {onSameUrlNavigation: 'reload'}),

here is the resolver

 @Injectable({
 providedIn: 'root'
 })
 export class LinkResolver implements Resolve<any> {

constructor(private router: Router, private dbservice: DbService) {}

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
    console.log('in the router');
    return this.dbservice.getBins();
}

and finally the service

getBins(): Observable<Bins[]> {
return this.http.get<Bins[]>('http://localhost:5000/api/values');
}
hlatimer
  • 73
  • 1
  • 9
  • can u subscribe and check whether you are getting getBins from the resolver itself ?? – Jameel Moideen Sep 07 '18 at 19:43
  • I ran the resolver directly in my component and I was able to subscribe and return values from my API `ngOnInit() { this.dbService.getBins().subscribe( response => { this.values = response; } ); this.linkresolver.resolve().subscribe(res => { this.bins = res; console.log(this.bins); }); }` – hlatimer Sep 07 '18 at 20:42

1 Answers1

0

The issue was in the app.component html template. Apparently you need to add the html tag <router-outlet></router-outlet> in order for the data to be routed to your view.

hlatimer
  • 73
  • 1
  • 9