One way to deal with getting and displaying data from an API is to route a user to a component, and then in that component’s ngOnInit hook call a method in a service to get the necessary data. While getting the data, perhaps the component can show a loading indicator. There’s another way however using what’s known as a route resolver, which allows you to get data before navigating to the new route.
server-resolver.service.ts
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Resolve, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {ServersService} from '../servers.service';
interface Server {
id: number;
name: string;
status: string;
}
@Injectable({
providedIn: 'root'
})
export class ServerResolverService implements Resolve<Server> {
constructor(private serversService: ServersService) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Server> | Promise<Server> | Server {
return this.serversService.getServer(+route.params['id']);
}
}
app.module.ts
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';
import {AppComponent} from './app.component';
import {HomeComponent} from './home/home.component';
import {UsersComponent} from './users/users.component';
import {ServersComponent} from './servers/servers.component';
import {UserComponent} from './users/user/user.component';
import {EditServerComponent} from './servers/edit-server/edit-server.component';
import {ServerComponent} from './servers/server/server.component';
import {ServersService} from './servers/servers.service';
import {RouterModule, Routes} from '@angular/router';
import {PageNotFoundComponent} from './page-not-found/page-not-found.component';
import {AuthService} from './auth.service';
import {AuthGuard} from './auth.guard';
import {CanDeactivateGuard} from './can-deactivate.guard';
import {ErrorPageComponent} from './error-page/error-page.component';
import {ServerResolverService} from './servers/server/server-resolver.service';
const appRoutes: Routes = [
{path: '', component: HomeComponent},
{
path: 'users', component: UsersComponent, children: [
{path: ':id/:name', component: UserComponent}]
},
{
path: 'servers', canActivateChild: [AuthGuard], component: ServersComponent, children: [
{path: ':id', component: ServerComponent, resolve: {server: ServerResolverService}},
{path: ':id/edit', component: EditServerComponent, canDeactivate: [CanDeactivateGuard]}
]
},
// {path: 'not-found', component: PageNotFoundComponent},
{path: 'not-found', component: ErrorPageComponent, data: {message: 'Page not found'}},
{path: '**', redirectTo: '/not-found'},
];
@NgModule({
declarations: [
AppComponent,
HomeComponent,
UsersComponent,
ServersComponent,
UserComponent,
EditServerComponent,
ServerComponent,
PageNotFoundComponent,
ErrorPageComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(appRoutes)
],
providers: [ServersService, AuthGuard, AuthService, CanDeactivateGuard, ServerResolverService],
bootstrap: [AppComponent]
})
export class AppModule {
}
server.component.ts
import {Component, OnInit} from '@angular/core';
import {ServersService} from '../servers.service';
import {ActivatedRoute, Data, Params, Router} from '@angular/router';
@Component({
selector: 'app-server',
templateUrl: './server.component.html',
styleUrls: ['./server.component.css']
})
export class ServerComponent implements OnInit {
server: { id: number, name: string, status: string };
constructor(private serversService: ServersService,
private route: ActivatedRoute,
private router: Router) {
}
ngOnInit() {
this.route.data.subscribe((value: Data) => {
this.server = value['server'];
});
// const id = +this.route.snapshot.params['id'];
// this.server = this.serversService.getServer(id);
// this.route.params.subscribe((params: Params) => {
// this.server = this.serversService.getServer(+params['id']);
// });
}
onEdit() {
this.router.navigate(['edit'], {relativeTo: this.route, queryParamsHandling: 'preserve'});
}
}
References
https://angular.io/guide/router#resolve-pre-fetching-component-data