【问题标题】:how can i add a loader while angular loads json data如何在角度加载 json 数据时添加加载器
【发布时间】:2020-10-30 20:20:54
【问题描述】:

这是我获取角度数据的代码, 我试图让它对用户更友好,加载需要一段时间,我正在寻找一种方法来添加 angular 加载器我是 angular 新手。

我正在使用 ionic cli 通过 json 数据构建移动应用程序提要

    import { Component, OnInit } from '@angular/core';
import { DataService } from "../data.service";

@Component({
  selector: 'app-articlespage',
  templateUrl: './articlespage.page.html',
  styleUrls: ['./articlespage.page.scss'],
})
export class ArticlespagePage implements OnInit {


    //id : any[];
    title : any[];
    author : any[];
    date : any[];
    content : any[];

  constructor(private dataService: DataService) { }

  ngOnInit() {
    this.dataService.getRemoteData().subscribe(data => {
      console.log("Remote Data:" + JSON.stringify(data));
      //console.log(data);
      this.parseJson(data);
    });
  }

  parseJson(data)
  {
     let jsonArray = data;

    //this.id = [];
    this.title = [];
    this.author = [];
    this.date = [];
    this.content = [];

    for(let i=0; i< jsonArray.length ; i++)
    {
       let jsonObject = jsonArray[i];
       //this.id.push(jsonObject.id);
       this.title.push(jsonObject.title);
       this.author.push(jsonObject.author.name);
       this.date.push(jsonObject.created_at );
       this.content.push(jsonObject.blog_entry );

    }
  }



}

html:

  <ion-content fullscreen>
     <h2 > Articles </h2>
      <ion-card class="feedspotcard-1 box" *ngFor=" let item of title; let i = index;">
        <ion-card-content>
            <div class="box-shadow">
              <div class="coolbar"></div>
              <!-- <p> <span> Id : </span> <span innerHTML={{id[i]}}> </span> </p> -->
              <h4> <span innerHTML={{title[i]}}> </span> </h4>
              <p>  <span innerHTML={{content[i]}}> </span> </p>
              <p> <span> author : </span> <span innerHTML={{author[i]}}> </span> </p>
              <p> <span> date : </span> <span innerHTML={{date[i]}}> </span> </p>
            </div>
        </ion-card-content>
      </ion-card>
    </ion-content>

不是必须的,但结果:

【问题讨论】:

    标签: json angular spinner loader


    【解决方案1】:

    您需要一个跟踪请求状态的属性。即loadingData: boolean

    在您的组件中,您需要在请求开始和完成时设置此值。

    ngOnInit(): void {
      this.loading = true;
      this.dataService.getRemoteData().subscribe(data => {
        // set the status of loading to false so the template can update
        this.loading =f alse;
        console.log("Remote Data:" + JSON.stringify(data));
        //console.log(data);
        this.parseJson(data);
      });
    }
    

    最后,在模板中,添加一个基于loading 属性值显示的元素。

    <div *ngIf="loading"><!-- Insert your progress message/animation --> </div>
    

    【讨论】:

    • 这似乎工作正常,直到我重新加载服务器我收到此错误:src/app/articlespage/articlespage.page.ts:21:10 中的错误:错误 TS2339:属性“加载”不存在于类型“ArticlespagePage”上。 21 this.loading = true; ~~~~~~~ src/app/articlespage/articlespage.page.ts:24:10 - 错误 TS2339:“ArticlespagePage”类型上不存在属性“正在加载”。 24 this.loading = false;
    • 好的,我通过在 nd 模块之前在脚本中标记 loading = "true" 解决了这个问题:export class ArticlespagePage implements OnInit { loading = true;
    • 这意味着该属性未在类中声明。在constructor() {} 之前,如果您添加loading: boolean = false;,这将确保该属性在组件初始化时可用。而且您不必在 OnInit 中重新初始化它。
    【解决方案2】:

    您可以使用ngx-spinner 作为加载程序。它的工作原理如下。

    首先在根模块(AppModule)中导入NgxSpinnerModule

    import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
    // Import library module
    import { NgxSpinnerModule } from "ngx-spinner";
     
    @NgModule({
      imports: [
        // ...
        NgxSpinnerModule
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
    })
    export class AppModule {}
    

    在任何你想使用 ngx-spinner 的地方添加 NgxSpinnerService 服务

     import { NgxSpinnerService } from "ngx-spinner";
         
        class AppComponent implements OnInit {
          constructor(private spinner: NgxSpinnerService) {}
         
          ngOnInit() {
            /** spinner starts on init */
            this.spinner.show();
         
            setTimeout(() => {
              /** spinner ends after 5 seconds */
              this.spinner.hide();
            }, 5000);
          }
        }
    

    在您的模板中使用。

     <ngx-spinner></ngx-spinner>
    

    【讨论】:

      猜你喜欢
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 2017-05-08
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多