1. 程式人生 > >Angular23 loading組件、路由配置、子路由配置、路由懶加載配置

Angular23 loading組件、路由配置、子路由配置、路由懶加載配置

瀏覽器 clas 遮罩 pes list ppm ppc () -s

1 需求

  由於Angular是單頁面的應用,所以在進行數據刷新是進行的局部刷新;在進行數據刷新時從瀏覽器發出請求到後臺響應數據是有時間延遲的,所以在這段時間就需要進行遮罩處理來提示用戶系統正在請求數據。

2 loading組件簡介

  loading組件就是專門負責遮罩處理的,可以自定一個loading組件,也可以使用別人創建號的loading模塊;loading組件生效後的效果如下:

  參考資料:點擊前往

    技術分享圖片

3 編程步驟

  3.1 創建一個angular項目

    技巧01:版本必須是angular4及以上

  3.2 創建一個組件

  3.3 創建一個user模塊

    技巧01:在user模塊中創建多個組件

  3.4 路由配置

    技巧01:每個模塊單獨設置路由配置文件

    技巧02:利用路由實現模塊懶加載

    3.4.1 子模塊路由配置文件

      技巧01:子模塊配置類中需要使用 forCild

        技術分享圖片

      技巧02:子模塊的配置文件配置好後需要在子模塊中引入配置文件,直接引入配置模塊中的那個類就行啦

        技術分享圖片

技術分享圖片
import { NgModule } from ‘@angular/core‘;
import { Routes, RouterModule } from ‘@angular/router‘;
import { UserListComponent } from 
‘./user-list/user-list.component‘; import { UserHomeComponent } from ‘./user-home/user-home.component‘; import { UserInfoComponent } from ‘./user-info/user-info.component‘; const routes: Routes = [ { path:‘‘, component:UserHomeComponent, children: [ { path:‘‘, redirectTo:
‘userList‘, pathMatch:‘full‘ }, { path:‘userList‘, component:UserListComponent }, { path:‘userInfo‘, component:UserInfoComponent } ] } ]; @NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) export class UserRoutingModule { }
user.routing.module.ts 技術分享圖片
import { NgModule } from ‘@angular/core‘;
import { CommonModule } from ‘@angular/common‘;

import { UserRoutingModule } from ‘./user-routing.module‘;
import { UserListComponent } from ‘./user-list/user-list.component‘;
import { UserInfoComponent } from ‘./user-info/user-info.component‘;
import { UserEditComponent } from ‘./user-edit/user-edit.component‘;
import { UserDetailComponent } from ‘./user-detail/user-detail.component‘;
import { UserListsComponent } from ‘./user-lists/user-lists.component‘;
import { UserHomeComponent } from ‘./user-home/user-home.component‘;

@NgModule({
  imports: [
    CommonModule,
    UserRoutingModule
  ],
  declarations: [UserListComponent, UserInfoComponent, UserEditComponent, UserDetailComponent, UserListsComponent, UserHomeComponent]
})
export class UserModule { }
user.module.ts

  3.4.2 根模塊路由配置

    技巧01:根模塊的路由配置文件中需要用 forRoot

      技術分享圖片

    技巧02:需要在根模塊中引入根路由配置類

      技術分享圖片

技術分享圖片
import { LoginComponent } from "./login/login.component";
import { NgModule } from "@angular/core";
import { RouterModule } from "@angular/router";


export const routes = [
    {
        path:‘‘,
        redirectTo:‘login‘,
        pathMatch:‘full‘
    },
    {
        path: ‘login‘,
        component: LoginComponent
    },
    {
        path:‘user‘,
        loadChildren:‘./user/user.module#UserModule‘
    },
    {
        path:‘**‘,
        component: LoginComponent
    }
]

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
  })
  export class AppRoutesModule { }
app.routes.module.ts 技術分享圖片
import { BrowserModule } from ‘@angular/platform-browser‘;
import { NgModule } from ‘@angular/core‘;

import { NgZorroAntdModule } from ‘ng-zorro-antd‘;
import { LoadingModule, ANIMATION_TYPES  } from ‘ngx-loading‘;

import { AppComponent } from ‘./app.component‘;
import { TestDemoComponent } from ‘./test-demo/test-demo.component‘;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { LoginComponent } from ‘./login/login.component‘;
import { RouterModule } from ‘@angular/router‘;
import { AppRoutesModule } from ‘./app.routes.module‘;


@NgModule({
  declarations: [
    AppComponent,
    TestDemoComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    LoadingModule.forRoot({
      animationType: ANIMATION_TYPES.wanderingCubes,
      backdropBackgroundColour: ‘rgba(0,0,0,0.1)‘, 
      backdropBorderRadius: ‘4px‘,
      primaryColour: ‘#ffffff‘, 
      secondaryColour: ‘#ffffff‘, 
      tertiaryColour: ‘#ffffff‘
  }),
    NgZorroAntdModule.forRoot(),
    AppRoutesModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
app.module.ts

  3.5 集成loading模塊

    3.5.1 下載相關依賴

npm install --save ngx-loading

    3.5.2 在模塊級別引入

      技巧01:loading模塊需要共享,所以需要在共享模塊或者跟模塊進行引入

        技術分享圖片

    3.5.3 在組件級別使用loading組件

      3.5.3.1 html編寫

        技術分享圖片

技術分享圖片
<div class="my-container">
  <ngx-loading [show]="loading" [config]="config"></ngx-loading>
  <h2>
    這是登錄頁面 
  </h2>
  <hr />
  
  <label for="username">用戶名</label>
  <input type="text" id="username" name="username" /> 
  <br />
  <label for="password">用戶密碼</label>
  <input type="password" id="password" name="password" />
  <button (click)="on_login_click()">登陸</button>
  
</div>
LoginComponent.html

      3.5.3.2 ts編寫

        技巧01:點擊登陸按鈕後,開啟遮罩;之後間隔5秒後交替開啟遮罩

技術分享圖片
import { Component, OnInit } from ‘@angular/core‘;
import { ANIMATION_TYPES } from ‘ngx-loading‘;

@Component({
  selector: ‘login‘,
  templateUrl: ‘./login.component.html‘,
  styleUrls: [‘./login.component.css‘]
})
export class LoginComponent implements OnInit {
  loading: boolean = false;
  config: object = {};

  private timer;

  constructor() {
    
  }

  ngOnInit() {
    this.config = {
      
      animationType: ANIMATION_TYPES.rectangleBounce,
      backdropBorderRadius: ‘0px‘,
      // backdropBackgroundColour: ‘#9f9ec8‘,
      fullScreenBackdrop: true,
      primaryColour: ‘skyblue‘,
      secondaryColour: ‘red‘
    }
  }

  on_login_click() {
    this.loading = true;
    this.timer = setInterval(
      () => {
        this.loading = !this.loading;
      },
      5000
    );
    alert("登陸");
  }

  ngOnDestroy() {
    if (this.timer) {
      alert(‘清除‘);
      clearInterval(this.timer);
    }
  }

}
LoginComponent.ts

  3.6 loading模塊源代碼

    參考資料 -> 點擊前往

  3.7 本博文源代碼

    獲取源代碼 -> 點擊前往

Angular23 loading組件、路由配置、子路由配置、路由懶加載配置