3
respostas

[Dúvida] Erro no NgFor

Arquivo app.component.html

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

Erro: error TS2339: Property 'url' does not exist on type 'object'.

app.component.ts

import { Component } from '@angular/core'; import { PhotoService } from './photos/photo/photo.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { photos:object[] = []; constructor(photoservice:PhotoService){ photoservice .listFromUser('flavio') .subscribe(photos=>this.photos=photos);

} }

app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component';

import { PhotosModule } from './photos/photos.module'; import { HttpClientModule } from '@angular/common/http'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, PhotosModule, HttpClientModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule {}

photo.component.html <img class="img-thumbnail" [src]="url" [alt]="description">

photo.component.ts import { Component, Input } from "@angular/core";

@Component({ selector: 'ap-photo', templateUrl:'photo.component.html' }) export class PhotoComponent { @Input() url = ''; @Input() description = ''; } photo.service.ts import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core';

const API = 'http://localhost:3000'; @Injectable({providedIn:'root'}) export class PhotoService{

constructor(private http:HttpClient){}

listFromUser(userName:string){

return this.http
.get<object[]>(API +'/flavio/photos');

} } photos.module.ts import { NgModule } from "@angular/core"; import { PhotoComponent } from './photo/photo.component';

@NgModule({ declarations:[PhotoComponent], exports:[PhotoComponent] //para tornar acessivel no app.module é necessario colocar o exports

}) export class PhotosModule{}

3 respostas

removi

Como posso corrigir esse erro?

Olá Wanderson, tudo bom?

O erro (Erro: error TS2339: Property 'url' does not exist on type 'object'.) se refere a propriedade url não existir no PhotoComponent.

Verifica se você criou essa propriedade e decorou ela com o @Input como no exemplo abaixo.

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

Caso o erro continue, volta aqui para procurarmos outra solução.

Abraço.

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