Adding API request

The solution for “Adding API request” can be found here. The following code will assist you in solving the problem.

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)    })  }}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in