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';

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


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/ - 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/ without skidding them to the constructor parameters?

Thanks in advance.

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:
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 - 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

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

This report looks clear
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 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:
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

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