Differenzansicht 05-di
im Vergleich zu 04-modules

← Zurück zur Übersicht | Demo | Quelltext auf GitHub
src/app/books/book-list/book-list.component.ts CHANGED
@@ -1,5 +1,6 @@
1
  import { Component, EventEmitter, Output } from '@angular/core';
2
 
 
3
  import { Book } from '../../shared/book';
4
 
5
  @Component({
@@ -11,27 +12,8 @@ export class BookListComponent {
11
  books: Book[] = [];
12
  @Output() selectBook = new EventEmitter<Book>();
13
 
14
- constructor() {
15
- this.books = [
16
- {
17
- isbn: '12345',
18
- title: 'Tierisch gut kochen',
19
- authors: ['Mrs Chimp', 'Mr Gorilla'],
20
- published: '2022-06-20',
21
- subtitle: 'Rezepte von Affe bis Zebra',
22
- thumbnailUrl: 'https://cdn.ng-buch.de/kochen.png',
23
- description: 'Immer lecker und gut'
24
- },
25
- {
26
- isbn: '67890',
27
- title: 'Backen mit Affen',
28
- authors: ['Orang Utan'],
29
- published: '2022-07-15',
30
- subtitle: 'Bananenbrot und mehr',
31
- thumbnailUrl: 'https://cdn.ng-buch.de/backen.png',
32
- description: 'Tolle Backtipps für Mensch und Tier'
33
- }
34
- ];
35
  }
36
 
37
  doSelect(book: Book) {
1
  import { Component, EventEmitter, Output } from '@angular/core';
2
 
3
+ import { BookStoreService } from '../../shared/book-store.service';
4
  import { Book } from '../../shared/book';
5
 
6
  @Component({
12
  books: Book[] = [];
13
  @Output() selectBook = new EventEmitter<Book>();
14
 
15
+ constructor(private service: BookStoreService) {
16
+ this.books = this.service.getAll();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  }
18
 
19
  doSelect(book: Book) {
src/app/shared/book-store.service.spec.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { BookStoreService } from './book-store.service';
2
+
3
+ describe('BookStoreService', () => {
4
+
5
+ let service: BookStoreService;
6
+
7
+ beforeEach(() => {
8
+ service = new BookStoreService();
9
+ });
10
+
11
+ it('should hold a hardcoded list of 2 books', () => {
12
+ const books = service.getAll();
13
+ expect(books).toHaveSize(2);
14
+ });
15
+ });
src/app/shared/book-store.service.ts ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Injectable } from '@angular/core';
2
+
3
+ import { Book } from './book';
4
+
5
+ @Injectable({
6
+ providedIn: 'root'
7
+ })
8
+ export class BookStoreService {
9
+ private books: Book[] = [];
10
+
11
+ constructor() {
12
+ this.books = [
13
+ {
14
+ isbn: '12345',
15
+ title: 'Tierisch gut kochen',
16
+ authors: ['Mrs Chimp', 'Mr Gorilla'],
17
+ published: '2022-06-20',
18
+ subtitle: 'Rezepte von Affe bis Zebra',
19
+ thumbnailUrl: 'https://cdn.ng-buch.de/kochen.png',
20
+ description: 'Immer lecker und gut'
21
+ },
22
+ {
23
+ isbn: '67890',
24
+ title: 'Backen mit Affen',
25
+ authors: ['Orang Utan'],
26
+ published: '2022-07-15',
27
+ subtitle: 'Bananenbrot und mehr',
28
+ thumbnailUrl: 'https://cdn.ng-buch.de/backen.png',
29
+ description: 'Tolle Backtipps für Mensch und Tier'
30
+ }
31
+ ];
32
+ }
33
+
34
+ getAll(): Book[] {
35
+ return this.books;
36
+ }
37
+ }