小编ske*_*on3的帖子

如何在 Angular 中的页面加载时发出 HTTP 请求(使用按钮)

当我的页面加载并在屏幕上显示响应文本时,我尝试向 API 发出 HTTP 请求。我正在使用 Angular 框架。

目前,当您按下按钮时,它会按照我的意愿工作。我想要按钮具有的确切功能,但它会在页面加载时自动发生。

//TypeScript
import { Component, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';


@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})

export class ProfileComponent implements OnInit {



posts: Observable<String[]>;

  constructor(private http:HttpClient) {

  }

 public getPosts() {

  this.posts = this.http.get<any[]>('https://jsonplaceholder.typicode.com/posts');

  }


ngOnInit() {
  }

}
Run Code Online (Sandbox Code Playgroud)
//TypeScript
import { Component, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { HttpClient, …
Run Code Online (Sandbox Code Playgroud)

json http angular

1
推荐指数
1
解决办法
8319
查看次数

标签 统计

angular ×1

http ×1

json ×1