loading 加载指示符

全局加载指示符,一般用于某个操作需要中断用户操作。

  1. import { LoadingModule } from '@delon/abc/loading';

代码演示

loading 加载指示符 - 图1

基础

最简单的用法。

  1. import { Component } from '@angular/core';
  2. import { LoadingService, LoadingType } from '@delon/abc/loading';
  3. @Component({
  4. selector: 'components-loading-basic',
  5. template: `
  6. <button nz-button (click)="show('icon')">Icon</button>
  7. <button nz-button (click)="show('spin')">Spin</button>
  8. <button nz-button (click)="show('text')">Only Text</button>
  9. `,
  10. })
  11. export class ComponentsLoadingBasicComponent {
  12. constructor(private loadingSrv: LoadingService) { }
  13. show(type: LoadingType): void {
  14. this.loadingSrv.open({ type });
  15. setTimeout(() => this.loadingSrv.close(), 1000 * 3);
  16. }
  17. }

loading 加载指示符 - 图2

自定义图标

设置 type: 'custom' 自定义加载指示符图标。

  1. import { DomSanitizer } from '@angular/platform-browser';
  2. import { Component } from '@angular/core';
  3. import { LoadingService } from '@delon/abc/loading';
  4. @Component({
  5. selector: 'components-loading-custom',
  6. template: `
  7. <button *ngFor="let i of customs" nz-button (click)="show(i)">{{ i.name }}</button>
  8. `,
  9. })
  10. export class ComponentsLoadingCustomComponent {
  11. customs = [
  12. {
  13. name: 'Balls',
  14. style: {
  15. height: '40px',
  16. fill: '#1890ff',
  17. },
  18. html: this.dom.bypassSecurityTrustHtml(`
  19. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  20. <path transform="translate(-8 0)" d="M4 12 A4 4 0 0 0 4 20 A4 4 0 0 0 4 12">
  21. <animateTransform attributeName="transform" type="translate" values="-8 0; 2 0; 2 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.25;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  22. </path>
  23. <path transform="translate(2 0)" d="M4 12 A4 4 0 0 0 4 20 A4 4 0 0 0 4 12">
  24. <animateTransform attributeName="transform" type="translate" values="2 0; 12 0; 12 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.35;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  25. </path>
  26. <path transform="translate(12 0)" d="M4 12 A4 4 0 0 0 4 20 A4 4 0 0 0 4 12">
  27. <animateTransform attributeName="transform" type="translate" values="12 0; 22 0; 22 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.45;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  28. </path>
  29. <path transform="translate(24 0)" d="M4 12 A4 4 0 0 0 4 20 A4 4 0 0 0 4 12">
  30. <animateTransform attributeName="transform" type="translate" values="22 0; 32 0; 32 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.55;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  31. </path>
  32. </svg>`),
  33. },
  34. {
  35. name: 'Bars',
  36. style: {
  37. fill: '#1890ff',
  38. },
  39. html: this.dom.bypassSecurityTrustHtml(`
  40. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  41. <path transform="translate(2)" d="M0 12 V20 H4 V12z">
  42. <animate attributeName="d" values="M0 12 V20 H4 V12z; M0 4 V28 H4 V4z; M0 12 V20 H4 V12z; M0 12 V20 H4 V12z" dur="1.2s" repeatCount="indefinite" begin="0" keytimes="0;.2;.5;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.8 0.4 0.8" calcMode="spline" />
  43. </path>
  44. <path transform="translate(8)" d="M0 12 V20 H4 V12z">
  45. <animate attributeName="d" values="M0 12 V20 H4 V12z; M0 4 V28 H4 V4z; M0 12 V20 H4 V12z; M0 12 V20 H4 V12z" dur="1.2s" repeatCount="indefinite" begin="0.2" keytimes="0;.2;.5;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.8 0.4 0.8" calcMode="spline" />
  46. </path>
  47. <path transform="translate(14)" d="M0 12 V20 H4 V12z">
  48. <animate attributeName="d" values="M0 12 V20 H4 V12z; M0 4 V28 H4 V4z; M0 12 V20 H4 V12z; M0 12 V20 H4 V12z" dur="1.2s" repeatCount="indefinite" begin="0.4" keytimes="0;.2;.5;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.8 0.4 0.8" calcMode="spline" />
  49. </path>
  50. <path transform="translate(20)" d="M0 12 V20 H4 V12z">
  51. <animate attributeName="d" values="M0 12 V20 H4 V12z; M0 4 V28 H4 V4z; M0 12 V20 H4 V12z; M0 12 V20 H4 V12z" dur="1.2s" repeatCount="indefinite" begin="0.6" keytimes="0;.2;.5;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.8 0.4 0.8" calcMode="spline" />
  52. </path>
  53. <path transform="translate(26)" d="M0 12 V20 H4 V12z">
  54. <animate attributeName="d" values="M0 12 V20 H4 V12z; M0 4 V28 H4 V4z; M0 12 V20 H4 V12z; M0 12 V20 H4 V12z" dur="1.2s" repeatCount="indefinite" begin="0.8" keytimes="0;.2;.5;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.8 0.4 0.8" calcMode="spline" />
  55. </path>
  56. </svg>`),
  57. },
  58. {
  59. name: 'Bubbles',
  60. style: {
  61. height: '40px',
  62. fill: '#1890ff',
  63. },
  64. html: this.dom.bypassSecurityTrustHtml(`
  65. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  66. <circle transform="translate(8 0)" cx="0" cy="16" r="0">
  67. <animate attributeName="r" values="0; 4; 0; 0" dur="1.2s" repeatCount="indefinite" begin="0"
  68. keytimes="0;0.2;0.7;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  69. </circle>
  70. <circle transform="translate(16 0)" cx="0" cy="16" r="0">
  71. <animate attributeName="r" values="0; 4; 0; 0" dur="1.2s" repeatCount="indefinite" begin="0.3"
  72. keytimes="0;0.2;0.7;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  73. </circle>
  74. <circle transform="translate(24 0)" cx="0" cy="16" r="0">
  75. <animate attributeName="r" values="0; 4; 0; 0" dur="1.2s" repeatCount="indefinite" begin="0.6"
  76. keytimes="0;0.2;0.7;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  77. </circle>
  78. </svg>`),
  79. },
  80. {
  81. name: 'Cubes',
  82. style: {
  83. height: '40px',
  84. fill: '#1890ff',
  85. },
  86. html: this.dom.bypassSecurityTrustHtml(`
  87. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  88. <path transform="translate(-8 0)" d="M0 12 V20 H8 V12z">
  89. <animateTransform attributeName="transform" type="translate" values="-8 0; 2 0; 2 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.25;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  90. </path>
  91. <path transform="translate(2 0)" d="M0 12 V20 H8 V12z">
  92. <animateTransform attributeName="transform" type="translate" values="2 0; 12 0; 12 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.35;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  93. </path>
  94. <path transform="translate(12 0)" d="M0 12 V20 H8 V12z">
  95. <animateTransform attributeName="transform" type="translate" values="12 0; 22 0; 22 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.45;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  96. </path>
  97. <path transform="translate(24 0)" d="M0 12 V20 H8 V12z">
  98. <animateTransform attributeName="transform" type="translate" values="22 0; 32 0; 32 0;" dur="0.8s" repeatCount="indefinite" begin="0" keytimes="0;.55;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" />
  99. </path>
  100. </svg>`),
  101. },
  102. {
  103. name: 'Cylon',
  104. style: {
  105. height: '40px',
  106. fill: '#1890ff',
  107. },
  108. html: this.dom.bypassSecurityTrustHtml(`
  109. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  110. <path transform="translate(0 0)" d="M0 12 V20 H4 V12z">
  111. <animateTransform attributeName="transform" type="translate" values="0 0; 28 0; 0 0; 0 0" dur="1.5s" begin="0" repeatCount="indefinite" keytimes="0;0.3;0.6;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  112. </path>
  113. <path opacity="0.5" transform="translate(0 0)" d="M0 12 V20 H4 V12z">
  114. <animateTransform attributeName="transform" type="translate" values="0 0; 28 0; 0 0; 0 0" dur="1.5s" begin="0.1s" repeatCount="indefinite" keytimes="0;0.3;0.6;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  115. </path>
  116. <path opacity="0.25" transform="translate(0 0)" d="M0 12 V20 H4 V12z">
  117. <animateTransform attributeName="transform" type="translate" values="0 0; 28 0; 0 0; 0 0" dur="1.5s" begin="0.2s" repeatCount="indefinite" keytimes="0;0.3;0.6;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  118. </path>
  119. </svg>`),
  120. },
  121. {
  122. name: 'Spin',
  123. style: {
  124. fill: '#1890ff',
  125. },
  126. html: this.dom.bypassSecurityTrustHtml(`
  127. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  128. <path opacity=".25" d="M16 0 A16 16 0 0 0 16 32 A16 16 0 0 0 16 0 M16 4 A12 12 0 0 1 16 28 A12 12 0 0 1 16 4"/>
  129. <path d="M16 0 A16 16 0 0 1 32 16 L28 16 A12 12 0 0 0 16 4z">
  130. <animateTransform attributeName="transform" type="rotate" from="0 16 16" to="360 16 16" dur="0.8s" repeatCount="indefinite" />
  131. </path>
  132. </svg>`),
  133. },
  134. {
  135. name: 'Spinning Bubbles',
  136. style: {
  137. fill: '#1890ff',
  138. },
  139. html: this.dom.bypassSecurityTrustHtml(`
  140. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  141. <circle cx="16" cy="3" r="0">
  142. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  143. </circle>
  144. <circle transform="rotate(45 16 16)" cx="16" cy="3" r="0">
  145. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.125s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  146. </circle>
  147. <circle transform="rotate(90 16 16)" cx="16" cy="3" r="0">
  148. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.25s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  149. </circle>
  150. <circle transform="rotate(135 16 16)" cx="16" cy="3" r="0">
  151. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.375s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  152. </circle>
  153. <circle transform="rotate(180 16 16)" cx="16" cy="3" r="0">
  154. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.5s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  155. </circle>
  156. <circle transform="rotate(225 16 16)" cx="16" cy="3" r="0">
  157. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.625s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  158. </circle>
  159. <circle transform="rotate(270 16 16)" cx="16" cy="3" r="0">
  160. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.75s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  161. </circle>
  162. <circle transform="rotate(315 16 16)" cx="16" cy="3" r="0">
  163. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.875s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  164. </circle>
  165. <circle transform="rotate(180 16 16)" cx="16" cy="3" r="0">
  166. <animate attributeName="r" values="0;3;0;0" dur="1s" repeatCount="indefinite" begin="0.5s" keySplines="0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8;0.2 0.2 0.4 0.8" calcMode="spline" />
  167. </circle>
  168. </svg>`),
  169. },
  170. {
  171. name: 'Spokes',
  172. style: {
  173. fill: '#1890ff',
  174. },
  175. html: this.dom.bypassSecurityTrustHtml(`
  176. <svg id="loading" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  177. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(0 16 16)">
  178. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0"/>
  179. </path>
  180. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(45 16 16)">
  181. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0.125s"/>
  182. </path>
  183. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(90 16 16)">
  184. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0.25s"/>
  185. </path>
  186. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(135 16 16)">
  187. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0.375s"/>
  188. </path>
  189. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(180 16 16)">
  190. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0.5s"/>
  191. </path>
  192. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(225 16 16)">
  193. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0.675s"/>
  194. </path>
  195. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(270 16 16)">
  196. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0.75s"/>
  197. </path>
  198. <path opacity=".1" d="M14 0 H18 V8 H14 z" transform="rotate(315 16 16)">
  199. <animate attributeName="opacity" from="1" to=".1" dur="1s" repeatCount="indefinite" begin="0.875s"/>
  200. </path>
  201. </svg>`),
  202. },
  203. ];
  204. constructor(private loadingSrv: LoadingService, private dom: DomSanitizer) { }
  205. show(custom: any): void {
  206. this.loadingSrv.open({
  207. type: 'custom',
  208. custom,
  209. text: 'Loading...(Auto close after 3s)',
  210. });
  211. setTimeout(() => this.loadingSrv.close(), 1000 * 3);
  212. }
  213. }

loading 加载指示符 - 图3

延迟

延迟显示 loading 效果。当调用 open()delay 时间内调用 close(),则不显示 loading 状态。

  1. import { Component } from '@angular/core';
  2. import { LoadingService } from '@delon/abc/loading';
  3. @Component({
  4. selector: 'components-loading-delay',
  5. template: `
  6. <button nz-button (click)="show()">Display after 1 seconds</button>
  7. <button nz-button (click)="close()">Close</button>
  8. `,
  9. })
  10. export class ComponentsLoadingDelayComponent {
  11. constructor(private loadingSrv: LoadingService) {}
  12. show(): void {
  13. this.loadingSrv.open({ delay: 1000 });
  14. setTimeout(() => this.loadingSrv.close(), 1000 * 5);
  15. }
  16. close() {
  17. this.loadingSrv.close();
  18. }
  19. }

API

LoadingService

名称说明
open(options?: LoadingShowOptions)打开
close()关闭

LoadingShowOptions

成员说明类型默认值全局配置
type显示类型LoadingTypespin
text描述文案string加载中…
icon类型为 icon 的配置项LoadingIcon-
custom类型为 custom 的配置项LoadingCustom-
delay延迟显示加载效果的时间(防止闪烁),单位:毫秒number-