I have a small problem. I want to pass data from my products, to my single product page. So I have a products page with 10-15 products to display with title of the product, small description, picture and a button for more info. When I click on that button I want to be redirected to that product page. So it says localhost:4200/product/productTitle1 and its picture, description and title can be used on that page like {{ title }}.
So I have products component.html
<div *ngFor="let product of productList.products" class="col-sm">
<div class="card mt-5" style="width: 20rem;">
<div class="card-body">
<h4 class="card-title">{{ product.title }}</h4>
<h6 class="card-subtitle mb-2 text-muted">{{ product.subtitle }}</h6>
<img [src]="product.imageUrl" class="card-text img-fluid">{{ product.cardText }}<br>
<a [routerLink]="['/product', product.title]" class="card-link btn btn-outline-primary btn-sm mt-3">More info</a>
<a href="#" class="card-link btn btn-info btn-sm mt-3">Add to cart</a>
</div>
</div>
with routerLink that shows product title in the link and redirect me to his product info page
[routerLink]="['/product', product.title]
and here is single product.component.ts
export class ProductComponent implements OnInit{
title: string;
subtitle: string;
cardText: string;
constructor(
private productList: ProductService,
private route: ActivatedRoute) { }
ngOnInit() {
let params = this.route.snapshot.paramMap;
this.title = params.get('title');
this.subtitle = params.get('subtitle');
this.cardText = params.get('cardText');
}
}
and product.service.ts that contain all product informations.
@Injectable()
export class ProductService {
products: any[] = [
{
id: 1,
title: 'Some title',
subtitle: 'Some subtitle',
imageUrl: './assets/images/main-image.jpg',
cardText: 'Some descriptive text'
},
{
id: 2,
title: 'Some title about second product',
subtitle: 'Some subtitle 2',
imageUrl: './assets/images/prod-cop.png',
cardText: 'Some text'
}, ...
The thing is that I can't see any information about that product on single product page except the {{ title }}. But I noticed that if I type like this routerLink =['/product', product] I can access all the information with snapshot but the link will contain all the information as well, which looks bad.
I am still beginner in Angular so I need your help.