import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class MyService {
constructor(private http: HttpClient) { }
getData(param: string) {
const url = `http://example.com/api/data?param=${param}`;
return this.http.get(url);
}
}
import { Component } from '@angular/core';
import { MyService } from './my.service';
@Component({
selector: 'app-my-component',
template: `
`,
})
export class MyComponent {
constructor(private myService: MyService) { }
getData() {
const param = 'example';
this.myService.getData(param).subscribe(data => {
console.log(data);
});
}
}