在Angular 2服务中创建typescript方法

时间:2016-12-22 18:02:52

标签: angular

我在typescript中创建了几个方法,但是现在我已将它们移动到Angular 2中,似乎我无法使用它们。

我甚至删除了作品function,但这也无效。以下是代码:

import {Injectable} from '@angular/core';

@Injectable()
export class BooksService {

let books: Array<IBook> = [];
let idNumber = 0;


function ID(): number{
  return idNumber++;
}


function generateBooks(): void {

    createBooks("Programming for dummies","Drew Jocham",Category.Programming, false);
    createBooks("Love story I won't read","Justin C",Category.Fiction, true);
    createBooks("Shooting for dummies","Autin Powers",Category.nonFiction, true);
    createBooks("Master Management","Dick Tracy",Category.nonFiction, false);
    createBooks("Advanced C++","Kevin Jocham",Category.Programming, true);
    createBooks("How to eat pizza","Ted",Category.nonFiction, true);

}

function clearBooks():void{
   books = [];
}

function createBooks(name:string,author:string,category:Category,isAvailable:boolean): void{

    let newBook:IBook = {id:ID(),name:name,author:author,category:category,isAvailable:isAvailable};

    books.push(newBook);
}

function getAvailbleFictionBooks(books): string[] {

    clearBooks();
    generateBooks();

    let availableBooks: Array<string> = [];

    for(let currentBook of books){

        if(currentBook.isAvailable && currentBook.catergory === Category.Fiction){

            availableBooks.push(currentBook);
        }
    }

    return availableBooks;
}

function getAvailbleProgrammingBooks(books): string[]{

    clearBooks();
    generateBooks();

    let availableBooks: Array<string>=[];

    for(let currentBook of books){

        if(currentBook.isAvailable && currentBook.catergory === Category.Programming){
            availableBooks.push(currentBook);
        }
    }

    return availableBooks;
}

function getAvailbleNonFictionBooks(books): string[] {

    clearBooks();
    generateBooks();

    let availableBooks: Array<string>=[];

    for(let currentBook of books){

        if(currentBook.isAvailable && currentBook.catergory === Category.nonFiction){
            availableBooks.push(currentBook);
        }
    }

    return availableBooks;
}

function getAllBooks(): Array<IBook> {

    clearBooks();
    generateBooks();

    return books;
}

function checkoutBook(id:number): boolean {

  let bookCollection = getAllBooks;

  for(let book of bookCollection()){

    if(book.id === id && book.isAvailable){
      let index = books.indexOf(book);
      delete books[index];
      return true;
    }
  }

  return false;
}

function checkInBook(book):void{
  books.push(book);
}

const allBooks = getAllBooks();

const availbleFictionBooks = allBooks.filter(book => getAvailbleNonFictionBooks(book));
const availbleProgrammingBooks = allBooks.filter(book => getAvailbleNonFictionBooks(book));
const availbleNonFictionBooks = allBooks.filter(book => getAvailbleNonFictionBooks(book));

for(let books of availbleFictionBooks){
    console.log(books);
}

}

我需要服务才能拥有这些功能,但显然我缺少了一些功能。

问题快照:

enter image description here

1 个答案:

答案 0 :(得分:0)

这是我立即注意到并修复的错误。

首先,您可以在导入中看到下划线路径,这意味着您没有正确的路径。

你错过了一堆这样的语法和一些语法错误。我已将的一些标记为代码中的注释。你真的应该首先开始学习Angular 2.我建议你查看Angular这是关于服务的教程:Tour of heroes这些教程非常有用! :)你的代码中有些东西可能会有所不同,但你需要知道的一切都在教程中! :)

parallel --sshloginfile ./my.nodes --bf ./myscript.sh ::: ./myscript.sh ./myscript.sh