Hier ist die Fehler, die ich sofort bemerkt und behoben.
Zuerst können Sie den Unterstreichungsweg in Ihrem Import sehen, das heißt, Sie haben nicht den richtigen Pfad.
Sie haben eine Reihe von diesen und einige Syntaxfehler vermisst. Ich habe einige von ihnen als Kommentare im Code markiert. Sie sollten wirklich anfangen, Angular 2 zu lernen. Ich schlage vor, Sie überprüfen Angular Und das ist das Tutorial zu Dienstleistungen: Tour of heroes Diese Tutorials sind wirklich hilfreich! :) Es gibt Dinge in deinem Code, die ich vielleicht anders mache, aber alles, was du wissen musst, findest du in den Tutorials! :)
import { Injectable } from '@angular/core';
import { Category } from 'yourPath'; // add the correct path here!
import { IBook } from 'yourPath'; // add the correct path here!
@Injectable()
export class BooksService {
books: Array<IBook> = [];
idNumber = 0;
ID(): number {
return this.idNumber++; // this was missing!
}
generateBooks(): void { // this missing on all!
this.createBooks("Programming for dummies", "Drew Jocham", Category.Programming, false);
this.createBooks("Love story I won't read", "Justin C", Category.Fiction, true);
this.createBooks("Shooting for dummies", "Autin Powers", Category.nonFiction, true);
this.createBooks("Master Management", "Dick Tracy", Category.nonFiction, false);
this.createBooks("Advanced C++", "Kevin Jocham", Category.Programming, true);
this.createBooks("How to eat pizza", "Ted", Category.nonFiction, true);
}
clearBooks(): void {
this.books = []; // this was missing!
}
createBooks(name: string, author: string, category: Category, isAvailable: boolean): void {
let newBook: IBook = { id: this.ID(), name: name, author: author, category: category, isAvailable: isAvailable };
this.books.push(newBook); // this was missing!
}
getAvailbleFictionBooks(books): string[] {
this.clearBooks(); // this was missing!
this.generateBooks(); // this was missing!
let availableBooks: Array<string> = [];
for (let currentBook of books) {
if (currentBook.isAvailable && currentBook.catergory === Category.Fiction) {
availableBooks.push(currentBook);
}
}
return availableBooks;
}
getAvailbleProgrammingBooks(books): string[] {
this.clearBooks(); // this was missing!
this.generateBooks(); // this was missing!
let availableBooks: Array<string> = [];
for (let currentBook of books) {
if (currentBook.isAvailable && currentBook.catergory === Category.Programming) {
availableBooks.push(currentBook);
}
}
return availableBooks;
}
getAvailbleNonFictionBooks(books): string[] {
this.clearBooks(); // this was missing!
this.generateBooks(); // this was missing!
let availableBooks: Array<string> = [];
for (let currentBook of books) {
if (currentBook.isAvailable && currentBook.catergory === Category.nonFiction) {
availableBooks.push(currentBook);
}
}
return availableBooks;
}
getAllBooks(): Array<IBook> {
this.clearBooks();
this.generateBooks();
return this.books;
}
checkoutBook(id: number): boolean {
let bookCollection = this.getAllBooks(); //error here!!
for (let book of bookCollection) { // You had an exta (here
if (book.id === id && book.isAvailable) {
let index = this.books.indexOf(book);
// delete books[index]; WRONG!
this.books.splice(index, 1); // CORRECT
return true;
}
}
return false;
}
checkInBook(book): void {
this.books.push(book);
}
}
'dies.ist etwas, das Sie brauchen, wenn Sie diese anrufen. Fügen Sie es an der Vorderseite hinzu, wo Sie die Funktionen aufrufen. 'this.createBooks (" ... 'Selbe Sache mit' books' und anderen lokalen Variablen. –
Look R. Richards Antwort und ich nehme an, Sie haben auch den Dienst injiziert wo Sie es brauchen? – Alex
Ich habe dies zu 'createBooks' hinzugefügt und das Wort 'function' entfernt, aber das hat nicht funktioniert Jetzt lege ich es zurück, wenn ich den Mauszeiger über die Funktion' Funktion erwartet Newline oder; ' – Drew1208