Home:ALL Converter>TypeScript fetch data from API

TypeScript fetch data from API

Ask Time:2019-03-19T20:26:34         Author:Daniel Faulkner

Json Formatter

I want the code below to be able to fetch JSON data from a simple API I have set up.

export class FaqFakeDb
{ 
    public static data = fetch('https://#######.co.uk/api/faq/');
}

This is for a FAQ page and its meant to be pulling data from the API. I am new to typescript and Angular so forgive me if I've made a simple mistake.

I want it to function like this:

export class FaqFakeDb
{
    public static data = [  
        {  
           "id":"1",
           "question":"test1",
           "answer":"test1"
        },
        {  
           "id":"2",
           "question":"test1",
           "answer":"test1"
        },
        {  
           "id":"3",
           "question":"test1",
           "answer":"test1"
        }
     ];
  }

Any help will me much appretiated :)

Author:Daniel Faulkner,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55241065/typescript-fetch-data-from-api
yy