Rxjs-working-with-rxjs-and-angular

提供:Dev Guides
移動先:案内検索

RxJSとAngularの操作

この章では、AngularでRxJを使用する方法について説明します。 ここでは、Angularのインストールプロセスについては説明しません。Angularのインストールについては、このリンクを参照してください-link:/angular7/angular7_environment_setup [https://www.finddevguides.com/angular7/angular7_environment_setup]

RxJSのAjaxを使用してデータをロードする例に直接取り組みます。

app.component.ts

import { Component } from '@angular/core';
import { environment } from './../environments/environment';
import { ajax } from 'rxjs/ajax';
import { map } from 'rxjs/operators'

@Component({
   selector: 'app-root',
   templateUrl: './app.componentl',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = '';
   data;
   constructor() {
      this.data = "";
      this.title = "Using RxJs with Angular";
      let a = this.getData();
   }
   getData() {
      const response =
      ajax('https://jsonplaceholder.typicode.com/users')
         .pipe(map(e => e.response));
      response.subscribe(res => {
         console.log(res);
         this.data = res;
      });
   }
}

app.componentl

<div>
   <h3>{{title}}</h3>
   <ul *ngFor="let i of data">
      <li>{{i.id}}: {{i.name}}</li>
   </ul>
</div>

<router-outlet></router-outlet>

このURLからデータをロードするRxJSのajaxを使用しました-https://jsonplaceholder.typicode.com/users。

あなたが表示をコンパイルすると、以下のようになります-

RxJs with Angular