Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Angular product-details.component.html El objeto es posiblemente 'indefinido'

Estoy siguiendo el tutorial Ver detalles del producto en el sitio web angular y encontré el error:

Error en src/app/product-details/product-details.component.html (4:16) El objeto posiblemente esté 'indefinido'.

No puedo pasar al siguiente tutorial porque necesita esta página para funcionar.

Aquí está el código:

lista-de-productos.componente.html

 <h2>Products</h2> <div *ngFor="let product of products"> <h3> <a [title]="product.name + ' details'" [routerLink]="['/products', product.id]"> {{ product.name }} </a> </h3> </div>

producto-detalles.componente.ts

 import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Product, products } from '../products'; @Component({ selector: 'app-product-details', templateUrl: './product-details.component.html', styleUrls: ['./product-details.component.css'] }) export class ProductDetailsComponent implements OnInit { product: Product|undefined; constructor( private route: ActivatedRoute, ) { } ngOnInit() { // First get the product id from the current route. const routeParams = this.route.snapshot.paramMap; const productIdFromRoute = Number(routeParams.get('productId')); // Find the product that correspond with the id provided in route. this.product = products.find(product => product.id === productIdFromRoute); } }

producto-detalles.componente.html

 <h2>Product Details</h2> <div *ngIf="product"> <h3>{{ product.name }}</h3> <h4>{{ product.price | currency }}</h4> <p>{{ product.description }}</p> </div>

Espero que alguien pueda ayudar a resolver el problema porque veo que el código está funcionando.

Gracias.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

También estoy haciendo este tutorial y lo arreglé agregando ProductDetailsComponent a las declaraciones dentro de app.module.ts.

 declarations: [ AppComponent, TopBarComponent, ProductListComponent, ProductAlertsComponent, ProductDetailsComponent //<-- add this into declarations ],

No olvide importar primero ProductDetailsComponent en app.module.ts.

 import { ProductDetailsComponent } from './product-details/product-details.component';

Debería funcionar después de actualizar todo el sitio web (no solo la pantalla a la derecha, vaya a guardar y actualice la pestaña del navegador).

over 4 years ago · Santiago Trujillo Report

0

Es posible que desee intentar usar product?.name

over 4 years ago · Santiago Trujillo Report

0

Lo arreglé agregando la importación 'ProductDetailsComponent' en 'app.module.ts'

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!