简体   繁体   中英

Injecting activateRoute class in Angular

I am refreing to the Book(pro-Angular-6) and I came across These Syntax

 constructor(private model:Model,activatedRoute:ActivatedRoute) {} 

i am confused with the Following points

  1. How can we use class Without injecting (what is difference between private activatedRoute:ActivatedRoute and activateRoute:ActivatedRoute)

  2. if we can just use as reference Type and limit the use of it inside the constructor, if it is fine i tried with the custom class inside my component i am getting null injection error

     class Emp { name='Bob' id = '12' } constructor(private model:Model,activatedRoute:ActivatedRoute,emp:Emp){}
  1. Why i am able to access activated only in the constructor if i Try to access in the Method i am getting error

    constructor(activateRoute:ActivatedRoute){ console.log(activatedRoute) //no error } someMethod() { console.log(activatedRoute) //error Did You mean ActivatedRoute console.log(this.activatedRoute) //error proprtey activateRoute Does not exist

    }

4)Moreover i am confused whether i am defining the type or injecting in the constructor with the following syntax constructor(activateRoute:ActivatedRoute){}

  1. Public dependencies can be accessed by the component's template where as Private dependencies can't be accessed by component templates .

Ex:

constructor(activateRoute:ActivatedRoute){}  

or

constructor(public activateRoute:ActivatedRoute){}

will not throw error while using in template as below

<div *ngIf="activateRoute">content goes here </div>
  1. In angular, we can inject only when a class is annotated with the injectable as below.

Ex:

    @injectable({
    providedIn:'root'
    })
     class Emp {
                name='Bob'
                id = '12'  
              }
    

export class AppComponent{
    constructor(private model:Model,activatedRoute:ActivatedRoute,emp:Emp){}
}

Your first question is about typescript. If you didn't specify access modifier on constructor parameters, it can be accessed only within constructor.

First 2 above explanations are justified

  1. I am not able to access any Where except in constructor, because in constructor i passed activateRoute:ActivatedRoute as signature to constructor so i can acess only in the constructor

4.we are refereeing to the Type (go confused with java script object Notation)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM