[英]Injecting activateRoute class in Angular
I am refreing to the Book(pro-Angular-6) and I came across These Syntax我正在阅读这本书(pro-Angular-6),我遇到了这些语法
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)
我们如何使用 class 而不注入
(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 我收到
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为什么我只能在构造函数中访问 activated 如果我尝试在方法中访问我收到错误
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){}
4)此外,我很困惑我是在定义类型还是使用以下语法在构造函数中注入
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.你的第一个问题是关于 typescript。如果你没有在构造函数参数上指定访问修饰符,它只能在构造函数中访问。
First 2 above explanations are justified上面的前两个解释是有道理的
activateRoute:ActivatedRoute
as signature to constructor so i can acess only in the constructoractivateRoute:ActivatedRoute
作为签名传递给构造函数,所以我只能在构造函数中访问4.we are refereeing to the Type (go confused with java script object Notation) 4.我们正在引用类型(与 java 脚本 object 符号混淆)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.