Angular Taskmgr 登陸

1、登陸module

一、先建立domain文件夾,在裏面建一個user.model.ts的user領域對象。html

export interface User{
    id?:string;
    email:string;
    password:string;
    name:string;
    avatar:string;
}

二、ng g m login建立一個login module.npm

在login下建立一個login組件。bootstrap

三、在login下創建一個login-routing.module.ts子路由。promise

【最好爲每一個模塊創建本身的路由】app

創建路由的快捷方式。裝插件Angulae7 Snippets,用ng-router-featuremodule.dom

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';

const routes: Routes = [
    { path: '', component: LoginComponent },
    { path: 'register', component: RegisterComponent }
];

@NgModule({
    imports: [CommonModule, RouterModule.forChild(routes)],

exports: [RouterModule]
})
export class LoginRoutingModule {}
View Code

四、配置根路由ide

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: '', redirectTo: '/login', pathMatch: 'full'
  },
  {
    path:'login',loadChildren:'./login/login.module#LoginModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],

  exports: [RouterModule]
})
export class AppRoutingModule { }
View Code

確保根模塊中引入根路由。svg

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatSidenavModule} from '@angular/material';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    CoreModule,
    MatSidenavModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
View Code

子模塊導入子路由。佈局

import { NgModule } from '@angular/core';
import { LoginComponent } from './login/login.component';
import { SharedModule } from '../shared/shared.module';
import { LoginRoutingModule } from './login-routing.module';

@NgModule({
  declarations: [LoginComponent],
  imports: [
    SharedModule,
    LoginRoutingModule
  ]
})
export class LoginModule { }
View Code

五、安裝animation動畫依賴flex

npm install --save @angular/animations

六、佈局

<form>
  <mat-card class="example-card">
    <mat-card-header>
      <mat-card-title>登陸:</mat-card-title>
    </mat-card-header>
    <mat-card-content>
      <mat-form-field class="example-full-width" class="full-width">
        <input type="text" matInput placeholder="您的email" style="text-align: right">
      </mat-form-field>
      <mat-form-field class="example-full-width" class="full-width">
        <input type="password" matInput placeholder="您的密碼" style="text-align: right">
      </mat-form-field>
      <button mat-raised-button type="button">登陸</button>

    </mat-card-content>
    <mat-card-actions class="text-right">
      <p>尚未帳戶?<a href="">註冊</a></p>
      <p>忘記密碼?<a href="">找回</a></p>
    </mat-card-actions>
  </mat-card>
</form>
View Code
form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}
mat-card {
  width: 20em;
  margin: 5px;
  flex: 0 0 20em;
}

.full-width {
  width: 100%;
}

.text-right {
  margin: 10px;
  text-align: end;
}
View Code

2、register Component 

一、在login下面新建register組件

ng g c login/register 

二、註冊組件頭像模塊使用svg

以前文章介紹過matIcon中可使用svg作爲資源。

經過addSvgIconSetInNamespace來添加圖標集

import { MatIconRegistry } from "@angular/material";
import { DomSanitizer } from "@angular/platform-browser";

export const loadSvgResources = (ir: MatIconRegistry, ds: DomSanitizer) => {
  const imgDir = "assets/img";
  const avatarDir = `${imgDir}/avatar`;
  const sidebarDir = `${imgDir}/sidebar`;
  const dayDir = `${imgDir}/days`;

  ir.addSvgIconSetInNamespace( //經過addSvgIconSetInNamespace來添加圖標集
    "avatars",
    ds.bypassSecurityTrustResourceUrl(`${avatarDir}/avatars.svg`)
  );
  ir.addSvgIcon(
    "day",
    ds.bypassSecurityTrustResourceUrl(`${sidebarDir}/day.svg`)
  );
  ir.addSvgIcon(
    "month",
    ds.bypassSecurityTrustResourceUrl(`${sidebarDir}/month.svg`)
  );
  ir.addSvgIcon(
    "project",
    ds.bypassSecurityTrustResourceUrl(`${sidebarDir}/project.svg`)
  );
  ir.addSvgIcon(
    "projects",
    ds.bypassSecurityTrustResourceUrl(`${sidebarDir}/projects.svg`)
  );
  ir.addSvgIcon(
    "week",
    ds.bypassSecurityTrustResourceUrl(`${sidebarDir}/week.svg`)
  );

  const days =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
  days.forEach((d)=>{
    ir.addSvgIcon(
      `day${d}`,
      ds.bypassSecurityTrustResourceUrl(`${dayDir}/day${d}.svg`)
    );
  });
};
View Code

在註冊頁面中

  ngOnInit() {
    const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
    this.items = nums.map(d => `avatars:svg-${d}`);
  }

在模版中

      <mat-grid-list cols="8">
        <mat-grid-tile *ngFor="let item of items">
<mat-icon [svgIcon]="item"></mat-icon>
        </mat-grid-tile>

 

 

3,修改樣式

讓icon變圓。

<form>
  <mat-card class="example-card">
    <mat-card-header>
      <mat-card-title>註冊:</mat-card-title>
    </mat-card-header>
    <mat-card-content>
      <mat-form-field class="full-width">
        <input type="text" matInput placeholder="您的姓名" style="text-align: right">
      </mat-form-field>
      <mat-form-field class="full-width">
        <input type="text" matInput placeholder="您的email" style="text-align: right">
      </mat-form-field>
      <mat-form-field class="full-width">
        <input type="password" matInput placeholder="您的密碼" style="text-align: right">
      </mat-form-field>
      <mat-form-field class="full-width">
        <input type="password" matInput placeholder="重複輸入您的密碼" style="text-align: right">
      </mat-form-field>
      <mat-grid-list cols="6">
        <mat-grid-tile *ngFor="let item of items">
          <mat-icon class="avatar" [svgIcon]="item"></mat-icon>
        </mat-grid-tile>
      </mat-grid-list>
      <button mat-raised-button type="button" color="primary">註冊</button>

    </mat-card-content>
    <mat-card-actions class="text-right">
      <p>尚未帳戶?<a href="">註冊</a></p>
      <p>忘記密碼?<a href="">找回</a></p>
    </mat-card-actions>
  </mat-card>
</form>
View Code
mat-icon.avatar {
  overflow: hidden;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  margin: 12px;
}

mat-card {
  width: 600px;
}
.full-width {
  width: 100%;
}

 

 

 

 

3、相關報錯TroubleShoting

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'login'
以前路由配置

AppRoutingModule裏這樣配置

{path: '', redirectTo: '/login', pathMatch: 'full'}

LoginRoutingModule裏這樣配置

{ path: 'login', component: LoginComponent }
修改成:
AppRoutingModule:
const routes: Routes = [
  {path: '', redirectTo: '/login', pathMatch: 'full'},
  {path:'login',loadChildren:'./login/login.module#LoginModule'}
];
LoginRoutingModule
{ path: '', component: LoginComponent }

具體可參考:lazy-loading-ngmodules 

未完待續。。。

相關文章
相關標籤/搜索