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

 

1 需求

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

 

2 loading組件簡介

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

  參考資料:點擊前往git

    

 

3 編程步驟

  3.1 建立一個angular項目

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

  3.2 建立一個組件

  3.3 建立一個user模塊

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

  3.4 路由配置

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

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

    3.4.1 子模塊路由配置文件

      技巧01:子模塊配置類中須要使用 forCild瀏覽器

        

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

        

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:根模塊的路由配置文件中須要用 forRootapp

      

    技巧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 本博文源代碼

    獲取源代碼 -> 點擊前往

相關文章
相關標籤/搜索