Making a GET request

Applications often request JSON data from the server. For example, the app might need a configuration file on the server. The ConfigService fetches this file with a get() method.

config.service.ts
import { elixor } from 'elixor';

const url = 'https://jsonplaceholder.typicode.com/posts';
export const getConfig = () => {
  return elixor.get(url);
}

A component, such as ConfigComponent,and calls the getConfig service method.

Config.tsx
import * as React from 'react';
import { getConfig } from './services/config.service';

class Config extends React.Component {

  componentDidMount() {
    getConfig()
      .subscribe(r => {
        console.log(r);
      });
  }
}

export default Config;

Because the service method returns an Observable of configuration data, the component subscribes to the method's return value.

Last updated