<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>404 Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/404/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/404/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 05 May 2022 11:31:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Handling not found routes in Angular</title>
		<link>https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/</link>
					<comments>https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/#respond</comments>
		
		<dc:creator><![CDATA[Pedro Lopes]]></dc:creator>
		<pubDate>Thu, 05 May 2022 11:31:15 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[404]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=12669</guid>

					<description><![CDATA[<p>In this post, we will see how we can handle 404 routes using a not found component in an Angular. There are two types of 404 routes that we want to handle. Static 404 &#8211; the URL the user navigates to doesn&#8217;t match any route Dynamic 404 &#8211; the URL matches a route, but the [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/">Handling not found routes in Angular</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this post, we will see how we can handle 404 routes using a not found component in an Angular.</p>



<p>There are two types of 404 routes that we want to handle.</p>



<ul class="wp-block-list"><li><strong>Static 404</strong> &#8211; the URL the user navigates to doesn&#8217;t match any route</li><li><strong>Dynamic 404</strong> &#8211; the URL matches a route, but the data associated with that route does not exist. For example, you navigate to a detail page with an ID, but no item exists with that ID.</li></ul>



<p>For this post, I assume that there is a component named <code>NotFoundComponent</code> that is the not found page to show the user. Replace this with whatever component</p>



<h2 class="wp-block-heading">Static 404 routes</h2>



<p>To set up a static 404 page, you need to add two routes to your <code>RoutingModule</code> configuration.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
const routes: Routes = &#x5B;
    // Other routes
    { path: &#039;404&#039;, component: NotFoundComponent },
    { path: &#039;**&#039;, component: NotFoundComponent }
]
</pre></div>


<p>We used a catchall route <code><em>'</em>**'</code> that matches whatever route didn&#8217;t match above, and shows our not found page.</p>



<p>Make sure to keep the catchall route at the end of your routes, otherwise the routes below it will not be accessible.</p>



<p>Another option here would&#8217;ve been to redirect the user to the <code>'404'</code> route, but that way you lose the context of the URL. If the user navigates back, he would go to the non-existing route and be redirected to the <code>'404'</code> path again. If this is the behavior you want, switch the last route with <code>{ path: '**', redirectTo: '404' }</code>.</p>



<h2 class="wp-block-heading">Dynamic 404 routes</h2>



<p>Now that we have the static 404 route configured, we can use the router to navigate to it whenever we want to show the not found page to the user.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
this.router.navigate(&#x5B;&#039;/404&#039;], { skipLocationChange: true });
</pre></div>


<p>By using the option <code>skipLocationChange: true</code>, we have the same behavior as mentioned above. Even though the router navigates to the route, the browser URL stays the same, allowing the user to easily go back.</p>



<h3 class="wp-block-heading">Personalizing the 404 message</h3>



<p>You may want to personalize the message shown on the 404 page. One way to do this is using the navigation state.</p>



<p>When navigating to the 404 page, you can include a <code>state</code> in the navigation options.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
this.router.navigate(&#x5B;&#039;/404&#039;], { 
    skipLocationChange: true,
    state: {
        // Whatever data you need on the 404 page
    	source: &#039;article&#039;,
        id: 123456
    }
});
</pre></div>


<p>On your <code>NotFoundComponent</code>, you can access this object in the constructor using the router&#8217;s <a href="https://angular.io/api/router/Router#getcurrentnavigation"><code>getCurrentNavigation</code></a> method.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
export class NotFoundComponent {
  constructor(private router: Router) {
    console.log(this.router.getCurrentNavigation().extras.state.source);
  }
}
</pre></div>


<p>You must access the state in the constructor, as it is only available while the router is navigating.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>In this post, we saw how to implement a 404 route in Angular. We looked at how we can redirect to it dynamically and even how to personalize the 404 page with route dependent data!</p>



<p>Have questions, corrections, or an alternative way of handling this? Leave a comment or reach out to me on Twitter <a href="https://twitter.com/pedronavelopes">@pedronavelopes</a> and let me know! Thanks for reading!</p>
<p>The post <a href="https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/">Handling not found routes in Angular</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
