Creating an Angular application

  1. Create the application:
    ng new keyboard-shortcuts
  1. Create multiple states (About, Dashboard, Home, and Profile) with basic templates under src/pages folder:
import { Component } from '@angular/core';

@Component({
selector: 'home',
template: 'home page'
})
export class HomeComponent {

}
  1. Create the routing for that state under <component_name>.routing.ts:
import { HomeComponent } from './home.component';

export const HomeRoutes = [
{ path: 'home', component: HomeComponent },
];

export const HomeComponents = [
HomeComponent
];
  1. Add the new routes and Components to the application's main routing file app.routing.ts next to app.module.ts:
import { Routes } from '@angular/router';
import {AboutComponents, AboutRoutes} from "./pages/about/about.routing";
import {DashboardComponents, DashboardRoutes} from "./pages/dashboard/dashboard.routing";
import {HomeComponents, HomeRoutes} from "./pages/home/home.routing";
import {ProfileComponents, ProfileRoutes} from "./pages/profile/profile.routing";

export const routes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
...AboutRoutes,
...DashboardRoutes,
...HomeRoutes,
...ProfileRoutes
];

export const navigatableComponents = [
...AboutComponents,
...DashboardComponents,
...HomeComponents,
...ProfileComponents
];
  1.  Register the routes with your application using RouterModule and declare your navigatableComponentsin the app.module.ts file:
@NgModule({
declarations: [
AppComponent,
...navigatableComponents
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(routes)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
  1. Create the HTML template to load the four routes in app.component.html:
<nav>
<button mat-button
routerLink="/about"
routerLinkActive="active">
About
</button>
<button mat-button
routerLink="/dashboard"
routerLinkActive="active">
Dashboard
</button>
<button mat-button
routerLink="/home"
routerLinkActive="active">
Home
</button>
<button mat-button
routerLink="/profile"
routerLinkActive="active">
Profile
</button>
</nav>

<router-outlet></router-outlet>

Once you have performed all the steps listed previously, run the following command in your Terminal; the web app should be up-and-running with four states for you to toggle:

ng serve
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.216.51