angular - Angular2 function call throws typeerror -


i have function in service, corresponding code:

    @injectable()     export class myservice { constructor(private http: http, private router: router) {}         getinfo() {             return this.http.get(url).map((response: response) => {return json.parse(response)}         }     } 

this method getting called ngoninit of component mentioned below:

export class mycomponent implements oninit {     constructor(private service: myservice) { }      ngoninit() {         this.service.getinfo().subscribe((data) => {                     this.info = data;                 });     } } 

the issue when navigating component, putting url on browser, works fine, when navigate component, using router.navigate method, throws exception,

getinfo not function

please correct me problem , doing wrong?

you should inject service component, use it. can that, declararing service parameter of component's constructor. example:

simple.service.ts

@injectable() export class simpleservice {     constructor(private http: http) {}     somemethod() { //some method } } 

some.component.ts

export class somecomponent implements oninit {     constructor(private simpleservice: simpleservice) { }      ngoninit() {         this.simpleservice.somemethod();     } } 

i'm not sure, if solution ")" character missing in service's method. should that:

return this.http.get(url).map((response: response) => {return json.parse(response)}) 

or even:

return this.http.get(url).map((response: response) => response.json()) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -