Skip to content

Commit 91e35b1

Browse files
committed
fix(angular): use a parametrised route to avoid conflict with routing
thanks @dhayab
1 parent eb56a07 commit 91e35b1

File tree

4 files changed

+5
-12
lines changed

4 files changed

+5
-12
lines changed

angular-instantsearch/using-instantsearch/src/app/app.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { HomeComponent } from './home/home.component';
44

55
export const routes: Routes = [
66
{ path: '', component: HomeComponent },
7-
{ path: 'product', component: ProductComponent },
7+
{ path: 'product/:objectID', component: ProductComponent },
88
];

angular-instantsearch/using-instantsearch/src/app/home/home.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<ul>
66
@for (hit of hits; track hit.objectID) {
77
<li>
8-
<a routerLink="/product" [queryParams]="{ objectID: hit.objectID }">
8+
<a routerLink="/product/{{ hit.objectID }}">
99
{{ hit.name }}
1010
</a>
1111
</li>

angular-instantsearch/using-instantsearch/src/app/product/product.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h1>{{ hit.name }}</h1>
77
<h2>Related Products</h2>
88
<ul>
99
<li *ngFor="let item of relatedItems">
10-
<a routerLink="/product" [queryParams]="{ objectID: item.objectID }">
10+
<a routerLink="/product/{{ item.objectID }}">
1111
{{ item.name }}
1212
</a>
1313
</li>

angular-instantsearch/using-instantsearch/src/app/product/product.component.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ export class ProductComponent {
2626
constructor(
2727
private route: ActivatedRoute,
2828
private InstantSearchService: InstantSearchService
29-
) {
30-
const objectID = route.snapshot.queryParamMap.get('objectID');
31-
if (!objectID) {
32-
throw new Error('objectID is required');
33-
}
34-
35-
this.replaceWidgets(objectID);
36-
}
29+
) {}
3730

3831
replaceWidgets(objectID: string) {
3932
if (this.objectID === objectID) {
@@ -58,7 +51,7 @@ export class ProductComponent {
5851
}
5952

6053
ngOnInit() {
61-
this.route.queryParams.subscribe((params) => {
54+
this.route.params.subscribe((params) => {
6255
this.replaceWidgets(params.objectID);
6356
});
6457
}

0 commit comments

Comments
 (0)