2016-12-22 4 views
0

Ich erstelle mehrere Methoden in Typoskript, aber jetzt, da ich sie in Angular 2 verschoben habe, scheint es, dass ich sie nicht verwenden kann.Erstellen von Typoskript-Methoden in einem Angular 2-Dienst

Ich entfernte sogar die Arbeit function, aber das hat auch nicht funktioniert. Hier ist der folgende Code:

Ich brauche den Dienst, um diese Funktionen zu haben, aber da ist offensichtlich etwas, das ich vermisse.

Ausgabe Schnappschuss:

enter image description here

+1

'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. –

+0

Look R. Richards Antwort und ich nehme an, Sie haben auch den Dienst injiziert wo Sie es brauchen? – Alex

+0

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

Antwort

0

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); 
    } 
} 
Verwandte Themen