angular2 call service via OnNgInit



If you need to call a service, please do it via OnNgInit instead of using constructor. If you doing unit testing, your code might not work.
As shown in the code below :

class SomeService implements OnInit {
  constructor(private http: Http) {

  }

  ngOnInit() {
    // this should hit mocked backend
    this.http.get("dummmy url").subscribe(v => {
      console.log('constructor subscribe hit');
    });
  }

  someMethod() {
    // this should hit mocked backend
    this.http.get("anotherurl").subscribe(v => {
      console.log('some method called subscribe hit');
    });
  }
}

Comments

Popular posts from this blog

gemini cli getting file not defined error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

vllm : Failed to infer device type