How to make API Calls the Right Way in Angular?

Step 1) Create a Common folder in your application.

export class Config {
  apiURL = 'https://localhost:44328/';

Step 2) In your module.ts file inject the class.

providers: [Security, MyAuthGuard, Config],

Step 3) Open your component.ts file. Inject the Config file in your component constructor.

export class AddComponent implements OnInit {
  constructor(public config: Config) {}

Step 4) In the same component write the code below, here in my code it is ADD METHOD.

AddRecord() {
    var observable =
      this.config.apiURL + 'Register/AddUser',
      (res) => this.Sucess(res),
      (res) => this.Error(res)

Leave a Comment