Mam problem z uruchomieniem funkcji z serwisu.
Komponent:
import { BookServicesService } from './../../services/book-services.service';
import { Component, OnInit, Injectable, NgModule } from '@angular/core';
@Component({
selector: 'app-add-book',
templateUrl: './add-book.component.html',
styleUrls: ['./add-book.component.css'],
})
@NgModule({
providers: [BookServicesService],
})
export class AddBookComponent implements OnInit {
nextBook = new newBook();
constructor(private _book: BookServicesService) { }
ngOnInit() {
}
onSubmit() {
console.log(this.nextBook);
this._book.addBook(this.nextBook);
}
}
class newBook {
constructor (
public title?: string,
public author?: string,
public content?: string
) {}
}
Serwis
import { Injectable } from '@angular/core';
import { Book } from '../Model/Book';
@Injectable({
providedIn: 'root'
})
export class BookServicesService {
booksArray = [];
constructor(
private book: Book
) { }
addBook(e) {
this.book.title = e.title;
this.book.author = e.author;
this.book.content = e.content;
this.book.public = new Date();
if (this.booksArray.length !== 0) {
this.book.id = (this.booksArray[this.booksArray.length - 1].id + 1);
} else {
this.book.id = 1;
}
this.booksArray.push(this.book);
console.log(this.book);
}
}
Uncaught (in promise): Error: Arguments array must have arguments.
i
Cannot read property 'addBook' of undefined
Szukam informacji, ale nie mogę odbugować tego.