I have a blog with articles. When I am on specific article on blog page (with url /blog/article-title) I have a spinner and make a get request, when it is successful, I show info related to this article. But if I type in url , for example, /blog/123 (I don't have this artilce) I have a spinner and error in console. What do I want is to show error page, but with incorrect url (blog/123) How can I do this?
app-routing.module
const routes: Routes = [
{
path: '',
loadChildren: () => import('./main-page/main-page.module').then(m => m.MainPageModule),
},
{
path: 'blog',
loadChildren: () => import('./blog-page/blog-page.module').then(m => m.BlogPageModule),
},
{
path: '**',
loadChildren: () => import('./error-page/error-page.module').then(m => m.ErrorPageModule),
},
];
blog-page-routing.module
export const routes: Route[] = [
{
path: '',
component: BlogPageComponent,
children: [
{
path: '',
component: ArticlesPageComponent,
},
{
path: ':id',
component: ArticlePageComponent,
},
],
},
];
article-page
public ngAfterViewInit(): void {
if (!this.article) {
this.articleSubscription = this.route.paramMap
.pipe(
switchMap(params => {
this.isLoadingInProgress = true;
return this.dataService.getArticleById(params.get('id'));
}),
)
.subscribe(response => {
this.article = response.currentArticle;
this.prevArticle = response.prevArticle;
this.nextArticle = response.nextArticle;
this.navigationModel = {
prevItem: {
route: this.prevArticle ? `blog/${this.prevArticle.id}` : null,
text: this.prevArticle && this.prevArticle.title,
image: this.prevArticle && this.prevArticle.mainImageUrl,
},
nextItem: {
route: this.nextArticle ? `blog/${this.nextArticle.id}` : null,
text: this.nextArticle && this.nextArticle.title,
image: this.nextArticle && this.nextArticle.mainImageUrl,
},
};
this.isLoadingInProgress = false;
this.changeDetection.markForCheck();
},
error => {
this.isLoadingInProgress = false;
});
}
}
UPDATED: The error page is shown when url is incorrect (localhost:4200/123) but it doesn't work with blog url (localhost:4200/blog/123)