Skip to content

Commit

Permalink
solve--use-router-with-component-input-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
CSchulz authored and Konstantin Pentarakis committed Jun 25, 2024
1 parent 2cefabf commit e11e3c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
import { provideRouter } from '@angular/router';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
providers: [provideHttpClient(), provideRouter(routes)]
providers: [
provideHttpClient(),
provideRouter(routes, withComponentInputBinding())
]
};
18 changes: 7 additions & 11 deletions src/app/book/book-detail/book-detail.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component } from '@angular/core';
import { Observable, switchMap } from 'rxjs';
import { Component, inject, Input } from '@angular/core';
import { Observable } from 'rxjs';
import { Book } from '../book';
import { ActivatedRoute } from '@angular/router';
import { BookApiService } from '../book-api.service';
import { AsyncPipe } from '@angular/common';

Expand All @@ -13,14 +12,11 @@ import { AsyncPipe } from '@angular/common';
styleUrl: './book-detail.component.scss'
})
export class BookDetailComponent {
book$: Observable<Book>;
private readonly bookApi = inject(BookApiService);
book$!: Observable<Book>;

constructor(
private readonly route: ActivatedRoute,
private readonly bookApi: BookApiService
) {
this.book$ = this.route.params.pipe(
switchMap(params => this.bookApi.getByIsbn(params?.['isbn']))
);
@Input({ required: true })
set isbn(isbn: string) {
this.book$ = this.bookApi.getByIsbn(isbn);
}
}

0 comments on commit e11e3c9

Please sign in to comment.