1. Install angular material, angular animations and angular flex layout
Copy npm install @angular/material @angular/cdk @angular/flex-layout @angular/animations --save
Copy import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NxModule } from '@nrwl/nx';
import { RouterModule } from '@angular/router';
import { authRoutes, AuthModule } from '@demo-app/auth';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; // Added
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule, // Added
NxModule.forRoot(),
RouterModule.forRoot([{path: 'auth', children: authRoutes}], { initialNavigation: 'enabled' }),
AuthModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
2. Add a new Nx lib to hold all the common material components we will use in our apps
Copy nx generate @nrwl/angular:lib material
libs/material/src/lib/material.module.ts
Copy import { NgModule } from '@angular/core';
import { FlexLayoutModule } from '@angular/flex-layout';
import { MatInputModule } from '@angular/material/input';
import { MatCardModule } from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatListModule } from '@angular/material/list';
import { MatIconModule } from '@angular/material/icon';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatMenuModule } from '@angular/material/menu';
import { MatTableModule } from '@angular/material/table';
import { MatSelectModule } from '@angular/material/select';
@NgModule({
imports: [
FlexLayoutModule,
MatInputModule,
MatCardModule,
MatButtonModule,
MatSidenavModule,
MatListModule,
MatIconModule,
MatToolbarModule,
MatProgressSpinnerModule,
MatMenuModule,
MatTableModule,
MatSelectModule
],
exports: [
FlexLayoutModule,
MatInputModule,
MatCardModule,
MatButtonModule,
MatSidenavModule,
MatListModule,
MatIconModule,
MatToolbarModule,
MatProgressSpinnerModule,
MatMenuModule,
MatTableModule,
MatSelectModule
]
})
export class MaterialModule {}
libs/auth/src/auth.module.ts
Copy import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Route } from '@angular/router';
import { LoginComponent } from './containers/login/login.component';
import { LoginFormComponent } from './components/login-form/login-form.component';
import { HttpClientModule } from '@angular/common/http';
import { MaterialModule } from '@demo-app/material'; // Added
export const authRoutes: Route[] = [
{ path: 'login', component: LoginComponent }
];
@NgModule({
imports: [CommonModule, RouterModule, HttpClientModule, MaterialModule], // Added
declarations: [LoginComponent, LoginFormComponent]
})
export class AuthModule {}
3. Add material default styles
apps/customer-portal/src/styles.scss
Copy @import '~@angular/material/prebuilt-themes/deeppurple-amber.css';
libs/auth/src/lib/components/login-form/login-form.component.html
Copy <mat-card>
<mat-card-title>Login</mat-card-title>
<mat-card-content>
<form fxLayout="column" fxLayoutAlign="center none">
<mat-form-field>
<input matInput placeholder="username" type="text" #username>
</mat-form-field>
<mat-form-field>
<input matInput placeholder="password" type="text" #password>
</mat-form-field>
</form>
<button mat-raised-button (click)="login({username:username.value, password:password.value})">login</button>
</mat-card-content>
</mat-card>
4. Go and explore flex layout docs