Angular2 — assignment in the constructor in TypeScript, how it works?

Good time of day.
Can someone explain how to correctly write these lines not passing them in the constructor parameters?

import { Router } from '@angular/router';
import { Injectable } from '@angular/core';

@Injectable()
export class ApiService {
 constructor(private router: Router) { }

get(){
this.router.navigate(['/test']);
}
}


I tried to rewrite the constructor so that would not pass the Http class using this service.
Get error and can not figure out how to correctly spell:

private router: Http;

constructor() {
 this.router = new Router();
}


How to rewrite the constructor and how to pass a parameter to it when the same scenario?

For example where you can show how to do it properly I give here the source.
It's interesting only two files:
app/route.click.ts - there is a method go() which go to the first page
app/layout/layout.component.ts - it inherits from the above class, and it is necessary to call the parent's method go().

How to Sangachal dependency app/route.click.ts without skidding them to the constructor parameters?

Thanks in advance.

upd:
Changed in this example, an Http Router that would make the example more detailed
July 2nd 19 at 13:06
2 answers
July 2nd 19 at 13:08
private _http: Http;

constructor(http: Http) {
 this._http = http;
}


Approach - Inversion of Control and Dependency Injection as an implementation approach

How does assign of property from the designer and why it is to do the: https://www.stevefenton.co.uk/2013/04/stop-manuall...
If you pass, then in this._http we will have the source code of the function. Only the code and call its methods, we can not. Because they're not there. - Reece.Beer13 commented on July 2nd 19 at 13:11
Example
https://pp.vk.me/c837734/v837734273/119e1/NAokKR46... - Reece.Beer13 commented on July 2nd 19 at 13:14
: you're talking about dependency injection in Angular2 read about the annotation @Injectable in particular - Yazmin_Schneid commented on July 2nd 19 at 13:17
: Trying to understand it.
But in addition to being fobbed off like this, nothing useful found.
For example here https://angular.io/docs/ts/latest/guide/dependency...

Did not understand where to pull ReflectiveInjector
that would make the injection within the methods.

This report looks clear pascalprecht.github.io/slides/di-in-angular-2
but again there are nuances which nobody really responds. - Reece.Beer13 commented on July 2nd 19 at 13:20
: again. Read about Inversion Of Control and why dependency injection was invented, what problems it solves and all that.

Now it seems to me that you want this principle a little "break" just like that. - Allene_Crona78 commented on July 2nd 19 at 13:23
July 2nd 19 at 13:10
Why do you need to injectate in method? This has some meaning?
I think what you want, you can do so take.ms/Jt2pn. You need to use this.inject, but there is the injector in the class for the decorator @Injectable() (@Directive and @Pipe inherit from @Injectable). When you injectate to the constructor is implicitly going on here is, very roughly:
@Injectable()
export class ApiService {
 private _http: Http
 constructor() {
 this.injector = ReflectiveInjector.resolveAndCreate([Http]); // automatically resolves dependencies and replace new Http()
 this._http = this.injector.get(Http) // get the singleton within the injector https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html
}
}

And as angular understands that this is not just a parameter, and di? In the injector there is a map in which recorded dependences, keys are types of the implementing classes, in this case Http.

In the official documentation it is written, understandable even with Google translator.
Thank you, I tried. Not working. Maybe where I went wrong, but not sure. So put planker that would be pointed if so...

By the way, in your example, there is a moment of documentation about ReflectiveInjector, but there is no description that it should be taken from
import { ReflectiveInjector } from '@angular/core';

And yet, the decorator @Injectable() does not create a method called this.inject.
Found this neither in the documentation nor in the debugger.

Doesn't seem like that is the decision.
Maybe the problem specifically with the router because it is not spelled out which classes he needed in the dependencies. But it is not yet clear. - Reece.Beer13 commented on July 2nd 19 at 13:13

Find more questions by tags JavaScriptTypeScriptOOP