简体   繁体   English

在Angular中注入activateRoute class

[英]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我对以下几点感到困惑

  1. 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)

  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 我收到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为什么我只能在构造函数中访问 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){}

  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.在 angular 中,只有当 class 被注解为injectable时,我们才能进行注入,如下所示。

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上面的前两个解释是有道理的

  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除了在构造函数中,我无法访问任何 Where,因为在构造函数中我将activateRoute: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.

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