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 本博文源代码

    获取源代码 -> 点击前往