📜  添加 API 请求 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:18.430000             🧑  作者: Mango

代码示例1
import { Component, OnInit } from '@angular/core';import { HttpClient } from '@angular/common/http';...import { Quote } from '../quote-class/quote';....export class GoalComponent implements OnInit {    goals:Goal[];  alertService:AlertService;  quote:Quote;....  constructor(goalService:GoalService, alertService:AlertService, private http:HttpClient) {    this.goals = goalService.getGoals()    this.alertService = alertService;  }  ngOnInit() {    interface ApiResponse{      author:string;      quote:string;    }    this.http.get("http://quotes.stormconsultancy.co.uk/random.json").subscribe(data=>{      // Succesful API request      this.quote = new Quote(data.author, data.quote)    })  }}