Contructor of an array of objects - Angular 2

0

I am using typescript to program in Angular2. I have an object car.ts the is as follows:

export class Car{
    name: String;
    door: {
        position: String;
        id: Number;
    };
}

I have initialized the object in the following way:

constructor() {
    this.door= new Door();
}
export class Door{
    position: String;
    ID: Number
}

And it works correctly. My problem comes when I try to initialize an array of objects:

export class Car{
    name: String;
    door: {
        position: String;
        id: Number;
    };
    color: [{
       one: String;
       two: String;

    }]
}

and I try it in the following way;

constructor() {
        this.color= new Color();
        this.door= new Door();
    }


export class Color{
    one: String;
    two: String;
}

The error is as follows:

  

Type Color is not assignable to type ...

I imagine the error is because the Receivers class does not create a Array

    
asked by Mario López Batres 17.01.2018 в 16:45
source

3 answers

0

Do it this way

Class:

export class Car{
    name: String;
    door: {
        position: String;
        id: Number;
    };
    color: Color[];
}

Constructor: (Empty array)

constructor() {
        this.color = new Array<Color>(); 
// De esta forma inicializa un array vació de Colores, y la variable ya tiene el tipo (Color[]) en su declaración
        this.door= new Door();
    }

Constructor: (Array with empty objects)

constructor() {
            this.color = [new Color() (, new Color(), ..)]
    // De esta forma inicializa un array con un objetos Color (vacios), o mas como se muestra entre paracentesis "()"
            this.door= new Door();
        }
    
answered by 17.01.2018 в 16:58
0

Your problem comes in several parts:

export class Car{
name: String;
door: {
    position: String;
    id: Number;
};
color: [{
   one: String;<---- esto deberia ser ',' en lugar de ';'
   two: String;

}]

}

At this point you just declared the 'color' member as an array that contains at least one object with 2 attributes (one and two). In order for your initialization code to work, it should be:

 constructor() {
    this.color = [{ one: "", two: "" }, new Color(), {one:"",two:""}]; 
    this.door= new Door();
}

where the first element may well be of the Color class or any object that satisfies the specified constraint (which has the attributes one and two only). If what you wanted was to specify an array of a specific type, then you can do it in several ways:

  color: {one:String, two:String }[];

or so:

  color: Color[];

or so:

  color:Array<{one:String, two:String }>

or so:

  color:Array<Color>

I hope this answers your question.

    
answered by 17.01.2018 в 19:43
0

Object In the end I created a array and not a tuple

export class Car{
    name: String;
    door: {
        position: String;
        id: Number;
    };
    color: {
       one: String;
       two: String;

    }[]
}

Constructor I have created a constructor in which push is made for each of the created elements. The number of elements created depends on a chosen variable each time that object is created.

constructor() {
        this.door= new Door();
        for (var i = 0; i < receivers; i++) {
            this.door.push(new Door);
        }
        this.color= [];
    }
export class Color{
    one: String;
    two: String;
}
    
answered by 19.01.2018 в 10:28