Component: This grabs the slug and fetches products, but slug is null server-side.
@Component({
selector: 'app-products-main',
imports: [CommonModule, RouterModule],
templateUrl: './products-main.component.html',
styleUrl: './products-main.component.scss',
})
export class ProductsMainComponent {
private productsService = inject(SsrProductsService);
private route = inject(ActivatedRoute);
private destroy$ = new Subject<void>();
productSlug: string | null = null;
products: CustomerProductResponse[] = [];
loading = true;
error: string | null = null;
ngOnInit(): void {
this.route.paramMap.pipe(takeUntil(this.destroy$)).subscribe((params) => {
this.productSlug = params.get('slug');
this.loadProducts();
});
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
private loadProducts(): void {
this.loading = true;
this.error = null;
const filters: CustomerProductRequest = {
pageNumber: 1,
pageSize: 10,
orderby: ['name'],
tagsSlugs: this.productSlug ? [this.productSlug] : [],
};
console.log('Loading products with filters:', filters);
this.productsService
.getProductsCustomer(filters)
.pipe(takeUntil(this.destroy$))
.subscribe({
next: (response) => {
this.products = response.data;
this.loading = false;
},
error: (err) => {
console.error('Error loading products:', err);
this.error = 'Failed to load products';
this.loading = false;
},
});
}
}```