Define variable object

0

I know the error is in how the user is defined, how would you define it as an object type?

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Proveedor1Provider } from '../../providers/proveedor1/proveedor1';
import { AboutPage } from '../about/about';
import { LoadingController } from 'ionic-angular';
import { ToastController } from 'ionic-angular';

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

    usuarios="";

    constructor(public toastCtrl: ToastController, public navCtrl: NavController, public provedor: Proveedor1Provider, public loadingCtrl: LoadingController) {}

    getItems(ev) {
        var val = ev.target.value;
        if(val.length > 1){

            this.provedor.obtenerDatos(val)
            .subscribe(
                (data)=> {this.usuarios = data},
                (error)=> {console.log(error);}
                )
        }
    }
}

    
asked by Emmanuel Lorenzo 28.07.2018 в 09:06
source

2 answers

0
  

TL; DR: usuarios: any = ''

You are having a TypeScript problem, not JavaScript. As you may already know, you are defining usuarios as string . To define it as type object you have to give it the type any .

In TypeScript you can define the type of variable explicitly and implicitly.

When you do usuarios = '' you are implicitly defining usuarios as string . You can also do this explicitly with usuarios: string = '' , that way you tell the TypeScript compiler what type of variable you will use.

Now to give it any value (like object ) you use : any (any means any).

    
answered by 01.08.2018 в 19:44
0

you have to create a user interface, for example:        interface Usuario { nombre: string; apellido: string }

then in your HomePage class you define your users variable in this way:      usuarios: Array<Usuario> = []; In this way you are indicating that the user variable is an array of the user type

    
answered by 04.08.2018 в 14:37