2016-06-11 8 views
13

Perché ciò avviene in Angular2 e Typescript?Come inizializzare un array in angular2 e typescript

export class Environment { 
    constructor(
     id: string, 
     name: string 
    ) { } 
} 


environments = new Environment('a','b'); 



app/environments/environment-form.component.ts(16,19): error TS2346: Supplied parameters do not match any signature of call target. 

Come si inizializza un array?

+0

È possibile utilizzare i dati pubblici: Array = []; –

risposta

4

Le definizioni di classe dovrebbe essere come:

export class Environment { 
    cId:string; 
    cName:string; 

    constructor(id: string, name: string) { 
     this.cId = id; 
     this.cName = name; 
    } 

    getMyFields(){ 
     return this.cId + " " + this.cName; 
    } 
} 

var environments = new Environment('a','b'); 
console.log(environments.getMyFields()); // will print a b 

Fonte: https://www.typescriptlang.org/docs/handbook/classes.html

1

io non pienamente cosa si intende veramente per inizializzare array?

Ecco un esempio

class Environment { 

    // you can declare private, public and protected variables in constructor signature 
    constructor(
     private id: string, 
     private name: string 
    ) { 
     alert(this.id); 
    } 
} 


let environments = new Environment('a','b'); 

// creating and initializing array of Environment objects 
let envArr: Array<Environment> = [ 
     new Environment('c','v'), 
     new Environment('c','v'), 
     new Environment('g','g'), 
     new Environment('3','e') 
    ]; 

Provalo qui: https://www.typescriptlang.org/play/index.html

0

Al fine di rendere più conciso è possibile dichiarare parametri del costruttore come public che creano automaticamente le proprietà con gli stessi nomi e queste proprietà sono disponibili via this :

export class Environment { 

    constructor(public id:number, public name:string) {} 

    getProperties() { 
    return `${this.id} : ${this.name}`; 
    } 
} 

let serverEnv = new Environment(80, 'port'); 
console.log(serverEnv); 

---result--- 
// Environment { id: 80, name: 'port' } 
32

È possibile utilizzare questo costrutto:

export class AppComponent { 

    title:string; 
    myHero:string; 
    heroes: any[]; 

    constructor() { 
     this.title = 'Tour of Heros'; 
     this.heroes=['Windstorm','Bombasto','Magneta','Tornado'] 
     this.myHero = this.heroes[0]; 
    } 
} 
6

è possibile creare e inizializzare array di qualsiasi oggetto come questo.

hero:Hero[]=[]; 
+0

come posso impostare 'hero' su una dimensione di 5? – JackSlayer94

+1

** vedi risposta in post sotto nella stessa discussione ** –

0

hi @ JackSlayer94 si prega di trovare l'esempio seguente per capire come fare un array di dimensione 5.

class Hero { 
 
    name: string; 
 
    constructor(text: string) { 
 
     this.name = text; 
 
    } 
 

 
    display() { 
 
     return "Hello, " + this.name; 
 
    } 
 

 
} 
 

 
let heros:Hero[] = new Array(5); 
 
for (let i = 0; i < 5; i++){ 
 
    heros[i] = new Hero("Name: " + i); 
 
} 
 

 
for (let i = 0; i < 5; i++){ 
 
    console.log(heros[i].display()); 
 
}