变量声明在“类"和“构造函数"之间有何不同? [英] How does variable declaration differ between the `class` and `constructor`?

查看:136
本文介绍了变量声明在“类"和“构造函数"之间有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一个例子,我正在尝试重现它.在constructor中添加的classservices(Injectable)内部声明nameage.

I have seen an example, and I am trying to reproduce it. The name and age are declared inside the class and services ( Injectable ) added in the constructor.

我想知道在这里用classconstructor声明变量之间的区别.任何人都可以帮助我了解差异.

I'd like to know the difference between declaring variable with class and constructor here. Any one help me to know the differences.

我也不能声明nameage而不是在constructor本身内部声明吗?

As well instead of declaring the name and age can't I declare inside of the constructor itself?

这是我的代码:

    import {Component} from 'angular2/core';
    import {CommonService} from './commonService';
    import {commonServiceIndipendent} from './commonSerivceIndipendent';

    @Component({

      selector : 'component1',

      template : `

        <h1>Component 1</h1>
        <input type="text" #message />

        <button (click)="onLog(message.value)" >Component1 - Message </button>

      `,

      providers:[commonServiceIndipendent]

    })

    export class Component1 {

      name:string; //why here?
      age:number; //why here?

//can't i add to constructor? if so how?
      constructor ( 
        private _commonService : CommonService, 
        private _commonServiceIndipendent:commonServiceIndipendent) {}


      //sending to same service. it has other instance in history
      onLog(message:string) {

        this._commonService.log( message );

        this.name = "Arif",
        this.age = 20;

        this.onData();

      }

      onData() {
        this._commonServiceIndipendent.myData(this.name,this.age);
      }

    }

推荐答案

在这种情况下

export class Component1 {

      constructor ( 
        private _commonService : CommonService, 
        private _commonServiceIndipendent:commonServiceIndipendent) {


       }

与此类似

export class Component1 {

      private _commonService : CommonService;
      private _commonServiceIndipendent:commonServiceIndipendent;

      constructor ( 
        _commonService : CommonService, 
        _commonServiceIndipendent:commonServiceIndipendent) {

        this._commonService = _commonService; 
        this._commonServiceIndipendent = _commonServiceIndipendent;

       }

如果未在构造函数protectedprivatepublic中使用,例如DI,则变量_commonService的范围是构造函数{ }的范围,不能使用来自另一个功能.

if you do not use in the constructor protected, private, or public, for example, DI, the range of the variable _commonService is the scope of the constructor { } you could not use from another function.

例如:

export class Component1 {

      constructor ( 
        _commonService : CommonService, 
        _commonServiceIndipendent:commonServiceIndipendent) {

          _commonService .... Work
       }

       foo(){

        _commonService ..... Cannot find name '_commonService'.
        this._commonService ..... Property '_commonService' does not exist on type 'yourClass'.  

       }

如果不将其分配给具有该类范围的另一个变量,则不能使用this关键字引用该变量.

If you not assign it to another variable that has the scope of the class,so you no could refer to this variable, with this keyword.

export class Component1 {

  name:string; //why here?
  age:number; //why here?

//can't i add to constructor? if so how?

在没有Angular2的打字稿中,您可以执行以下操作:

in typescript without Angular2, you can do this with nothing more:

constructor (name:string, age:number) {}

但是在带有Angular2的打字稿中,Angular2通常负责在这里使用DI作为示例:

but in typescript with Angular2, Angular2 is responsible, most of the time, to make use of DI for exaple here:

constructor (private _commonServiceIndipendent:commonServiceIndipendent){}

您用于该providers:[commonServiceIndipendent].

Angular2:注入非@Injectable类

如何使用依赖项注入(DI )在Angular2中正确显示了?

这篇关于变量声明在“类"和“构造函数"之间有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆