51
loading...
This website collects cookies to deliver better user experience
library/:location/books/:selectedBookId
or...?searchString=jon&secondParameter=1
.ActivatedRouteSnapshot
:export class YourComponent implements OnInit {
private yourPathParam: number;
private yourQueryParam: string;
constructor(private readonly activatedRouteSnapshot: ActivatedRouteSnapshot) {}
ngOnInit() {
const pathParams: Params[] = this.activatedRouteSnapshot.params;
this.yourPathParam = pathParams['yourPathParam'];
const queryParams: Params[] = this.activatedRouteSnapshot.queryParams;
this.yourQueryParam = queryParams['yourQueryParam'];
}
}
ActivatedRoute
instead of the ActivatedRouteSnapshot
:this.activatedRoute.params.subscribe((newPathParams) => console.log(newPathParams));
this.activatedRoute.queryParams.subscribe((newQueryParams) => console.log(newQueryParams));
Router
:this.router.navigateByUrl(newUrl);
npm install @jdrks/ngx-deep-linking
oryarn add @jdrks/ngx-deep-linking
@Input
and @Output
for the fields you want to deep link.
@Input
to initially populate the component field and change it if the url changes@Output
to change the url whenever that component output emits. Make sure to follow the naming convention for Angular two way data binding by appending 'Change' to the field name.DeepLinkingRoute
for a type safe configuration
import {DeepLinkingRoute, DeepLinkingWrapperComponent} from '@jdrks/ngx-deep-linking';
...
const routes: DeepLinkingRoute[] = [
{
path: 'books/:selectedBookId',
component: DeepLinkingWrapperComponent,
wrappedComponent: BookListComponent,
deepLinking: {
params: [{name: 'selectedBookId', type: 'number'}],
queryParams: [{name: 'searchString', type: 'string'}],
}
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class BookListRoutingModule {
}
@Component({
templateUrl: 'book-list.component.html'
})
export class BookListComponent {
private _selectedBookId: number = 0;
@Output()
private selectedBookIdChange: EventEmitter<number> = new EventEmitter<number>();
@Input()
get selectedBookId(): number {
return this.selectedBookId;
}
set selectedBookId(value: number) {
this.selectedBookId = value;
this.selectedBookIdChange.next(bookId);
}
private _searchString = '';
@Output()
searchStringChange: EventEmitter<string> = new EventEmitter<string>();
@Input()
get searchString(): string {
return this._searchString;
}
set searchString(value: string) {
this._searchString = value;
this.searchStringChange.next(value);
}
...
}
@Output
fields when using the setters, but this is just convenience.DeepLinkingWrapperComponent
. The fields that should be "deep linked" are synced with the DeepLinkingWrapperComponent
via "manual" two-way data binding. The wrapper reacts to changes on both url and wrapped component and updates vice versa.