【问题标题】:How to show spinner (or anything else) on page load in Angular 5?如何在 Angular 5 中的页面加载时显示微调器(或其他任何内容)?
【发布时间】:2018-07-19 16:49:27
【问题描述】:

我正在尝试在页面加载时显示图像(一般的想法是显示微调器)但它没有显示:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-form',
    templateUrl: './form.component.html'
})
export class FormComponent implements OnInit {

    loading = true;

    constructor() { }

    ngOnInit() {
        this.loading = true;
    }

}

在html中:

<app-form>
    <img *ngIf="loading" src="/assets/img/logo.png" />
</app-form>

form.component 模板位于&lt;app&gt; 模块中。

【问题讨论】:

    标签: angular angular5 spinner onload


    【解决方案1】:

    我在我目前正在进行的许多项目中都这样做。最好的办法是。

    在您的form.component.ts

    import { Component, OnInit } from '@angular/core';
    
    @Component({
        selector: 'app-form',
        templateUrl: './form.component.html'
    })
    export class FormComponent implements OnInit {
    
        loading = true;
        loaded = false;
    
        constructor() { }
    
        async ngOnInit() {
            try {
                await this.getData(); // DataCall
            } catch (err) {
                console.log('Error', err);
            }
            this.loaded = true;
            this.loading = false;
        }
    
    }
    

    然后在你的form.component.html

    <img *ngIf="!loading" src="/assets/img/logo.png" />
    <ng-container *ngIf="loaded">
        ...
        // Page Content
        ...
    </ng-container>
    

    【讨论】:

      【解决方案2】:

      form.component.html的html改成:

      <img *ngIf="loading" src="/assets/img/logo.png" />
      

      不需要&lt;app-form&gt;&lt;/app-form&gt;,放在父组件的html中

      【讨论】:

      • 它现在显示但只与页面的其他内容一起显示,我想在加载时显示它,同时等待页面加载。可能吗? @jcroll
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多