NGX Progressbar 教程
2026-01-17 09:29:32作者:董斯意
1. 项目介绍
NGX Progressbar 是一个轻量级的 Angular 模块,用于在网页加载过程中展示进度条。该项目提供多种功能,包括对 Http 请求、路由事件的支持,以及创建多个独立的进度条实例。它允许自定义配置,如颜色、速度等,并与 Angular 应用程序无缝集成。
2. 项目快速启动
安装依赖
首先,你需要安装 ngx-progressbar 到你的 Angular 项目中:
npm install ngx-progressbar
引入模块
接着,在你的主模块(比如 app.module.ts)中导入所需模块:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { NgProgressModule, NgProgressHttpModule } from 'ngx-progressbar';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
RouterModule.forRoot([]),
NgProgressModule.forRoot(), // 全局配置
NgProgressHttpModule, // 针对 Http 请求的自动跟踪
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
使用组件
在你的组件模板中添加 <ng-progress> 标签:
<ng-progress></ng-progress>
自动跟踪 HTTP 请求
如果你正在使用 Angular 的 HttpClient,NgProgressHttpModule 可以自动追踪请求并更新进度条:
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { NgProgressService } from 'ngx-progressbar';
@Injectable()
export class MyService {
constructor(private http: HttpClient, private ngProgress: NgProgressService) {}
getData() {
this.ngProgress.start();
return this.http.get('http://example.com/data').pipe(
finalize(() => this.ngProgress.complete())
);
}
}
3. 应用案例和最佳实践
- 定制样式:你可以通过 CSS 对进度条进行定制,例如改变背景颜色或进度条的颜色。
- 多实例:在一个页面上可以创建多个进度条,每个都有自己的 ID:
<ng-progress id="instance1"></ng-progress>
<ng-progress id="instance2"></ng-progress>
- 手动控制:你可以使用组件提供的方法来控制进度条:
<ng-progress #progressBar></ng-progress>
<button (click)="progressBar.start()">开始</button>
<button (click)="progressBar.inc(10)">增加10%</button>
<button (click)="progressBar.complete()">完成</button>
4. 典型生态项目
@ngx-progressbar/core: 提供核心进度条组件和服务。@ngx-progressbar/http: 支持跟踪 Http 请求的模块。@ngx-progressbar/router: 监听路由变化并显示加载进度的模块。
以上是 NGX Progressbar 的基本介绍和使用方式,它可以帮助你在开发中为用户提供更好的体验。更多详细信息和高级用法,建议查看官方仓库中的文档和示例。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。00
weapp-tailwindcssweapp-tailwindcss - bring tailwindcss to weapp ! 把 tailwindcss 原子化思想带入小程序开发吧 !TypeScript00
CherryUSBCherryUSB 是一个小而美的、可移植性高的、用于嵌入式系统(带 USB IP)的高性能 USB 主从协议栈C00
热门内容推荐
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
583
3.96 K
Ascend Extension for PyTorch
Python
413
493
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
361
230
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
暂无简介
Dart
823
203
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
906
722
昇腾LLM分布式训练框架
Python
125
150
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.42 K
798
React Native鸿蒙化仓库
JavaScript
316
368