在Angular 7中,可以通过使用ngx-loading插件来实现在加载时显示占位图像。
首先,安装ngx-loading插件:
npm install ngx-loading --save
然后,在需要显示占位图像的组件中引入LoadingModule:
import { LoadingModule } from 'ngx-loading';
@NgModule({
...
imports: [
...
LoadingModule
],
...
})
export class MyComponentModule { }
接下来,在组件类中定义一个布尔类型的变量来控制占位图像的显示与隐藏:
import { Component } from '@angular/core';
@Component({
...
})
export class MyComponent {
public loading = false;
loadData() {
this.loading = true;
// 模拟异步加载数据
setTimeout(() => {
this.loading = false;
}, 3000);
}
}
最后,在组件的HTML模板中使用ngx-loading指令来显示占位图像:
在上面的示例中,当调用loadData方法时,loading变量将被设置为true,从而显示占位图像。在加载数据完成后,loading变量将被设置为false,占位图像将被隐藏,显示加载完成后的内容。
请确保将占位图像的路径替换为正确的图像路径。
上一篇:Angular7: 如果模型与JSON不一致,如何正确地映射数据进行订阅可观察对象的方法。
下一篇:Angular7@ViewChilderrorCannotreadpropertiesofundefined(reading'next')