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
How can we use class Without injecting (what is difference between private activatedRoute:ActivatedRoute and activateRoute:ActivatedRoute)
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){}
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){}
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>
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
activateRoute:ActivatedRoute
as signature to constructor so i can acess only in the constructor4.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.