Routing means moving from one page to another page. Like Home, About, Contact-Us are the examples of links we mostly see in any website. Now, suppose user is at Home page and when user click over About button(link) that navigate us to About page, means it displayed us the content of About page and likewise. Simply when user click the browser’s back and forward buttons and the browser navigates backward and forward through the history of pages we have seen. @angular/router is a JavaScript package that built and maintained by the Angular core team. The Angular router is a core part of the Angular platform. It enables developers to build Single Page Applications with multiple views and allow navigation between these views. The Router-Outlet is a directive that's available in router library @angular/router. Based on the current browser's URL path the component is inserted into the <router-outlet>. Suppose we have a routing for about 👇👇 Now, when the user opens localhost:port/about URL the AboutComponent will be inserted in <router-outlet>. This <router-outlet> is an HTML tag, we can paste this in app.component.html like this👇👇 We can have multiple outlets in our Angular application which enables us to implement advanced routing scenarios. (Scroll down to read more about MULTIPLE OUTLETS AND AUXILIARY ROUTES) RouterModule.forRoot(routes) method is used to navigate the user to a specific view. we can have multiple routes like this👇👇
Route-Guards allows or denies the user access to the route pages.
routerLink is a directive to create navigation links this directive takes the associated path and navigate to the component. For example: The Angular Router match this path from routing and takes us to the matched component: (In this case /about this will take us to AboutComponent 👇👇) We can have multiple outlets in same application like this:👇👇 A component has one primary route and we can have auxiliary routes. We need to name a router outlet to create an auxiliary route then the component associated with the auxiliary route will be displayed.
Next in the router navigation we need to specify the outlet attribute, like this: #6 FINAL CODEWe will create routing module. #6.2 IMPORTING THE ROUTING MODULENext, we need to import this routing module into the main app module. #6.3 ADDING THE ROUTER OUTLETFinal step is to add the router outlet. The Angular router replaces this <router-outlet></router-outlet> with the component that matched in your routing. So, in above case when user opens /about page the <router-outlet></router-outlet> tag replaced by the template of AboutComponent. We can also redirect to another route, this is how we can do that👇👇 Here we want to match the exact empty path, that’s why we specify the full match strategy. Conclusion
you wouldn’t mind giving it some claps 👏 👏 since it helped, I’d greatly appreciate it :) Help others find the article, so it can help them! Always be clapping… Originally published at https://www.codewithchintan.com on December 27, 2019. |