<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Francesco Di Stefano on Medium]]></title>
        <description><![CDATA[Stories by Francesco Di Stefano on Medium]]></description>
        <link>https://medium.com/@francesco.dstf?source=rss-77db3a87926b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nw7ZVTxbKyOfiMnt.jpg</url>
            <title>Stories by Francesco Di Stefano on Medium</title>
            <link>https://medium.com/@francesco.dstf?source=rss-77db3a87926b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 03:56:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@francesco.dstf/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Build an interface with Login and SideNav using Angular Material 7 (and Lazy Loading)]]></title>
            <link>https://medium.com/@francesco.dstf/build-an-interface-with-login-and-sidenav-using-angular-material-7-and-lazy-loading-fb9d99813589?source=rss-77db3a87926b------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb9d99813589</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[materials]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[sidebar]]></category>
            <category><![CDATA[login]]></category>
            <dc:creator><![CDATA[Francesco Di Stefano]]></dc:creator>
            <pubDate>Wed, 15 May 2019 15:49:31 GMT</pubDate>
            <atom:updated>2020-02-12T07:40:55.086Z</atom:updated>
            <content:encoded><![CDATA[<h3>Build an interface with Login and SideNav using Angular Material 8 and 7 (and Lazy Loading)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ucywbrvbv7POp5I5VuPuqA.png" /><figcaption>Interface generated using MatFortyTwo boilerplate. More info at the end of the article…</figcaption></figure><p><strong>EDIT: </strong>The only difference for this tutorial between Angular 8 and 7 is the way Lazy Loaded Modules are imported. There are some steps where I will give you two different solution. <br><strong>Please be sure to use the right one for the version of Angular you are using!</strong></p><p>In this article you will find a small guide, step by step, on how to generate a web application with a Login page and a main SideNav component for internal navigation after the login. The main focus will be on how to structure the navigation to have everything running smoothly using best practices (not on how to style the UI).</p><h4><strong>If you want something ready to use, scroll to the end of this article.</strong></h4><h3>Project Initialization</h3><p>First, we will need to create a new Angular CLI project (<a href="https://cli.angular.io/">Install Angular CLI</a>)</p><pre>ng new sideNav --routing</pre><p>Now we can install Angular Material</p><pre>cd ./sideNav<br>npm install --save @angular/material @angular/cdk @angular/animations</pre><p>Include Angular Material Animations Module in <strong><em>app.module.ts</em></strong></p><pre>import {BrowserAnimationsModule} from &#39;@angular/platform-browser/animations&#39;;<br><br>@NgModule({<br>  ...<br>  imports: [...,BrowserAnimationsModule],<br>  ...<br>})<br>export class AppModule { }</pre><p>Include a theme in <strong><em>src/styles.scss</em></strong> file</p><pre>@import &quot;~@angular/material/prebuilt-themes/indigo-pink.css&quot;; </pre><p>Now let’s prepare our root component (app)<br>Replace <strong><em>src/app/app.component.html </em></strong>content with this:</p><pre>&lt;div <em>class</em>=&quot;app-view&quot;&gt;<br>    &lt;router-outlet&gt;&lt;/router-outlet&gt;<br>&lt;/div&gt;</pre><p>Add this to <strong><em>src/app/app.component.scss</em></strong>:</p><pre>.app-view {<br>    height: 100vh;<br>    width: 100vw;<br>}</pre><p>Add this to <strong><em>src/styles.scss</em></strong></p><pre>body {<br>    margin: 0 <em>!important</em>;<br>}</pre><h3>Login Page Creation</h3><p>Use angular-cli to create a new component (with routing) inside a new <strong><em>pages </em></strong>folder. (Run these commands from the root of the project)</p><pre>ng g m pages/login --routing<br>ng g c pages/login</pre><blockquote>Creating a module with routing, will help us with the Lazy Loading configuration and also to prevent the main route module from becoming too crowded, once we have a lot of routes.</blockquote><p>Replace the content of :<br> — <strong><em>src/app/pages/login/login.component.html</em></strong> with this:</p><pre>&lt;div class=&quot;full-page&quot;&gt;<br>&lt;mat-card class=&quot;login-card&quot;&gt;<br>    &lt;mat-card-title&gt;<br>        Login to App<br>    &lt;/mat-card-title&gt;<br>    &lt;mat-card-content&gt;<br>        &lt;mat-form-field <em>class</em>=&quot;login-field&quot;&gt;<br>            &lt;input <em>matInput placeholder</em>=&quot;Email&quot; <em>name</em>=&quot;email&quot; <em>[(ngModel)]</em>=&quot;email&quot; <em>type</em>=&quot;email&quot;/&gt;<br>        &lt;/mat-form-field&gt;<br>        &lt;mat-form-field <em>class</em>=&quot;login-field&quot;&gt;<br>            &lt;input <em>matInput placeholder</em>=&quot;Password&quot; <em>name</em>=&quot;password&quot; <em>[(ngModel)]</em>=&quot;password&quot; <em>type</em>=&quot;password&quot;/&gt;<br>        &lt;/mat-form-field&gt;<br>        <br>        <br>    &lt;/mat-card-content&gt;<br>    &lt;mat-card-actions&gt;<br>        &lt;button <em>mat-raised-button color</em>=&quot;primary&quot;<br>                <em>class</em>=&quot;login-field&quot; <em>(click)</em>=&quot;login(email, password)&quot; <em>type</em>=&quot;submit&quot;&gt;<br>            Login<br>        &lt;/button&gt;<br>    &lt;/mat-card-actions&gt;<br>&lt;/mat-card&gt;<br>&lt;/div&gt;</pre><p>— <strong><em>src/app/pages/login/login.component.scss</em></strong> with this:</p><pre>.full-page{<br>  height: 100%;<br>  display: flex;<br>}<br><br>.login-card {<br>  margin: auto;<br>  width: 300px;<br>}<br><br>.login-field {<br>  width: 100%;<br>}</pre><p>Add imports to <strong><em>src/app/pages/login/login.module.ts</em></strong></p><pre>imports: [..., MatCardModule, MatInputModule, MatButtonModule, FormsModule]</pre><p>Add data models and login function to <strong><em>src/app/pages/login/login.component.ts</em></strong></p><pre><em>export class </em>LoginComponent <em>implements OnInit </em>{<br>    <br>    email: <em>string</em>;<br>    password: <em>string</em>;</pre><pre><em>    constructor</em>(<em>private </em>router: Router) {}</pre><pre>    public login(email: string, password: string) {<br>        <em>this</em>.router.navigate([&#39;sidenav&#39;], { replaceUrl: <em>true </em>});<br>    }</pre><pre>    ngOnInit() {}<br>}</pre><blockquote>We skip any login logic, the focus of this article is to deal with the navigation after all. If you feel the need for it, please leave a request on the comments and I will cover this argument in a future article.</blockquote><p>Add the new route to <strong><em>src/app/pages/login/login-routing.module.ts</em></strong></p><pre><em>const </em>routes: <em>Routes </em>= [{ path: &#39;&#39;, component: LoginComponent }];</pre><p>This way, the new route will know which component to render, when matched.<br>Now that we have this new piece of “modularized” routing, we just have to define a way to access it in the main routing file.<br>We can do it by adding this to <strong><em>src/app/app-routing.module.ts</em></strong></p><p><strong>EDIT: </strong>With Angular 8, changes the way we import LazyLoaded Modules. Be sure to use the right of the following methods, depending on which version you are using.</p><pre><em>// ANGULAR 7<br>const </em>routes: <em>Routes </em>= [{<br>  path: &#39;&#39;,<br>  loadChildren: &#39;./pages/login/login.module#LoginModule&#39;<br>}];</pre><pre>// ANGULAR 8<br><em>const </em>routes: <em>Routes </em>= [{<br>  path: &#39;&#39;,<br>  loadChildren: () =&gt;<br>    import(&#39;./pages/login/login.module&#39;).then(<br>      m =&gt; m.LoginModule<br>    )<br>}];</pre><p>This is the way you define a lazy-loaded route. Just add a reference to a route module on the loadChildren attribute of a specific path.<br>Now you can give it a try, run</p><pre>ng serve --open</pre><p>and you should see our newly created login page. It should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*3oAkYHFIK6nhJhoY_oum4w.png" /></figure><h3>SideNav Parent Component Creation</h3><p>Now we need a component with a sidenav that will be able to render inside of the sidenav-content every next route that we are going to hit. To do that, we first generate a new component.</p><pre>ng g m components/sidenav<br>ng g c components/sidenav</pre><p>Now add this new component to the main routing file <br><strong><em>src/app/app-routing.module.ts</em></strong></p><pre><em>const </em>routes: <em>Routes </em>= [...,<br>{path: &#39;sidenav&#39;,component:SidenavComponent}<br>]</pre><pre>@NgModule({imports:[...,SideNavModule],...})</pre><p>Now replace the content of <strong><em>src/app/components/sidenav/sidenav.component.html </em></strong>with:</p><pre>&lt;mat-sidenav-container&gt;<br>  &lt;mat-sidenav <em>opened mode</em>=&quot;side&quot;&gt;<br>    &lt;mat-nav-list&gt;<br>      &lt;a <em>mat-list-item [routerLink]</em>=&quot;[&#39;page-one&#39;]&quot;&gt;Page One&lt;/a&gt;<br>      &lt;a <em>mat-list-item [routerLink]</em>=&quot;[&#39;page-two&#39;]&quot;&gt;Page Two&lt;/a&gt;<br>      &lt;a <em>mat-list-item [routerLink]</em>=&quot;[&#39;page-three&#39;]&quot;&gt;Page Three&lt;/a&gt;<br>    &lt;/mat-nav-list&gt;<br>  &lt;/mat-sidenav&gt;<br>  &lt;mat-sidenav-content&gt;<br>    &lt;router-outlet&gt;&lt;/router-outlet&gt;<br>  &lt;/mat-sidenav-content&gt;<br>&lt;/mat-sidenav-container&gt;</pre><p>And of <strong><em>src/app/components/sidenav/sidenav.component.css </em></strong>with:</p><pre>mat-sidenav-content {<br>  height: 100vh;<br>}</pre><p>Finally, import the modules needed in <strong><em>src/app/components/sidenav/sidenav.module.ts:</em></strong></p><pre>imports: [..., MatSidenavModule,RouterModule,MatListModule]</pre><p>Now, you should be able to see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*3bQaP6qhuZ9Qrrj7O6JT_A.png" /></figure><blockquote>Don’t worry if clicking on the links causes an error, it’s normal. We will fix it in the next step</blockquote><h3>SideNav Pages Creation</h3><p>Now it’s time to render some pages inside of our sidenav content. To do that, first we create three new components in the pages folder</p><pre>ng g m pages/page-one --routing<br>ng g c pages/page-one<br>ng g m pages/page-two --routing<br>ng g c pages/page-two<br>ng g m pages/page-three --routing<br>ng g c pages/page-three</pre><p>Now add the new components to their local route modules<br> — <strong><em>src/app/pages/page-one-routing.module.ts<br></em></strong> — <strong><em>src/app/pages/page-two-routing.module.ts<br></em></strong> — <strong><em>src/app/pages/page-three-routing.module.ts</em></strong></p><pre><em>#page-one<br>const </em>routes: <em>Routes </em>= [{path: &#39;&#39;, component: PageOneComponent}];<br>#page-two<br><em>const </em>routes: <em>Routes </em>= [{path: &#39;&#39;, component: PageTwoComponent}];<br>#page-three<br><em>const </em>routes: <em>Routes </em>= [{path: &#39;&#39;, component: PageThreeComponent}];</pre><p>Last step we need is to define these new routes as children of the sidenav route. To do that, we add them to <strong><em>src/app/app-routing.module.ts</em></strong></p><p><strong>EDIT: </strong>With Angular 8, changes the way we import LazyLoaded Modules. Be sure to use the right of the following methods, depending on which version you are using.</p><pre><em>// ANGULAR 7<br>const </em>routes: <em>Routes </em>= [<br>  ...,<br>  {<br>    path: &#39;sidenav&#39;,<br>    component: SidenavComponent,<br>    children: [<br>      {<br>        path: &#39;page-one&#39;,<br>        loadChildren: &#39;./pages/page-one/page-one.module#PageOneModule&#39;<br>       },<br>       {<br>         path: &#39;page-two&#39;,<br>         loadChildren: &#39;./pages/page-two/page-two.module#PageTwoModule&#39;<br>       },<br>       {<br>         path: &#39;page-three&#39;,<br>         loadChildren: &#39;./pages/page-three/page-three.module#PageThreeModule&#39;<br>       }<br>    ]  <br>}];</pre><pre>// ANGULAR 8<br><em>const </em>routes: <em>Routes </em>= [<br>  ...,<br>  {<br>    path: &#39;sidenav&#39;,<br>    component: SidenavComponent,<br>    children: [<br>      {<br>        path: &#39;page-one&#39;,<br>        loadChildren: () =&gt;<br>          import(&#39;./pages/page-one/page-one.module&#39;).then(<br>            m=&gt;m.PageOneModule<br>          )<br>       },<br>       {<br>         path: &#39;page-two&#39;,<br>         loadChildren: () =&gt; <br>           import(&#39;./pages/page-two/page-two.module&#39;).then(<br>             m=&gt;m.PageTwoModule<br>           )<br>       },<br>       {<br>         path: &#39;page-three&#39;,<br>         loadChildren: () =&gt;<br>           import(&#39;./pages/page-three/page-three.module&#39;).then(<br>             m=&gt;m.PageThreeModule<br>           )<br>       }<br>    ]  <br>  }<br>];</pre><pre>     ]  <br>}];</pre><p>Now your pages are lazy loaded inside of the sidenav-content, as intended!</p><h3>Conclusions</h3><p>The reason why we can navigate to a different component, while keeping the parent “alive”, is that we have defined a child router-outlet inside of the main one (located in the app component). This way Angular Router will render the parent component (sidenav in this case) using the main router-outlet and move the rendering of any child route to the child router-outlet. Of course you can have as many nested router-outlets as you want, but remember to keep the same hierarchy also in the routes that you will define.</p><p>I think this is a good way of dealing with navigation stacks in angular for a lot of applications, that’s why I created a template that will take care of all the things covered in this article (and much more), including automated generation of pages with a very useful script. If you are interested in that, just clone this repo :</p><p>(<a href="https://github.com/francescodist/matFortyTwo"><strong>MatFortyTwo</strong> — https://github.com/francescodist/matFortyTwo)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb9d99813589" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>