这是我的html代码,将显示书名和作者姓名,当然还有后退按钮。
<div class="row">
<div class="col-xs-12">
<h1>{{book.title}}</h1>
<h3>{{book.author}}</h3>
<button class="btn btn-default" (click)="onBack()">Back</button>
</div>
</div>
这是我的单书打字稿。
import { Component, OnInit } from '@angular/core';
import {Book} from '../../models/Book.model';
import {ActivatedRoute, Router} from '@angular/router';
import {BooksService} from '../../services/books.service';
@Component({
selector: 'app-single-book',
templateUrl: './single-book.component.html',
styleUrls: ['./single-book.component.scss']
})
export class SingleBookComponent implements OnInit {
book: Book;
constructor(private route: ActivatedRoute,
private booksService: BooksService,
private router: Router) { }
ngOnInit() {
this.book = new Book('', '');
const id = this.route.snapshot.params[' id'];
this.booksService.getSingleBook(+id).then(
(book: Book) => {
this.book = book;
}
);
}
onBack() {
this.router.navigate(['/books']);
}
}
这是我的打字稿,如下所示:(book-form.component)
import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {BooksService} from '../../services/books.service';
import {Router} from '@angular/router';
import {Book} from '../../models/Book.model';
@Component({
selector: 'app-book-form',
templateUrl: './book-form.component.html',
styleUrls: ['./book-form.component.scss']
})
export class BookFormComponent implements OnInit {
bookForm: FormGroup;
constructor(private formBuilder: FormBuilder,
private booksService: BooksService,
private router: Router) { }
ngOnInit() {
this.initForm();
}
initForm() {
this.bookForm = this.formBuilder.group({
title: ['', Validators.required],
author: ['', Validators.required]
});
}
onSaveBook() {
const title = this.bookForm.get('title').value;
const author = this.bookForm.get('author').value;
const newBook = new Book (title, author);
this.booksService.createNewBook(newBook);
this.router.navigate(['/book']);
}
}
我不明白怎么了,所有代码看起来都不错。 我不明白控制台中的消息。