Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

[Dúvida] Property 'url' does not exist on type 'Object'.ngtsc(2339)

O meu dá os seguintes erros no App.Component.html

Property 'url' does not exist on type 'Object'.ngtsc(2339) app.component.ts(5, 12): Error occurs in the template of component AppComponent.

Property 'description' does not exist on type 'Object'.ngtsc(2339) app.component.ts(5, 12): Error occurs in the template of component AppComponent.

app.component.html>>>

<ap-photo *ngFor="let photo of photos" [url]="photo.url" [description]="photo.description">

app.component.ts>>>

import { HttpClient } from '@angular/common/http'; import { Component } from '@angular/core';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] })

export class AppComponent { title = 'OtherPic';

photos: Object[] = [];

constructor(http: HttpClient) { http .get<Object[]>('http://localhost:3000/flavio/photos') .subscribe( photos =>{ console.log(photos); this.photos = photos }); } }

2 respostas
solução!

Consegui resolver era só colocar

photos: Object[] = []

da seguinte maneira

photos: any[] = []

O erro está ocorrendo porque o TypeScript não sabe o tipo de dados das propriedades 'url' e 'description' do objeto 'photo', que é do tipo 'Object'. Para corrigir isso, você pode criar uma interface que defina o tipo de dados do objeto 'photo' e usar essa interface para tipar a propriedade 'photos' no seu componente.

Aqui está um exemplo de como você pode fazer isso:

import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';

interface Photo {
  url: string;
  description: string;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'OtherPic';
  photos: Photo[] = [];

  constructor(http: HttpClient) {
    http
      .get<Photo[]>('http://localhost:3000/flavio/photos')
      .subscribe(photos => {
        console.log(photos);
        this.photos = photos;
      });
  }
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software