我正在尝试按照教程进行操作,但我遇到了编译器错误,因为它需要一个带有构造函数的标准类,但在教程中,这是如何完成的。也许自教程发布以来情况发生了变化,但我无法找到创建教程的人。我想完成它。
我收到的错误消息:
“不能将 'new' 与类型缺少调用或构造签名的表达式一起使用。”
我是 angular 的新手,请帮助我。
bookstore-front/src/app/book-detail/book-detail.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Book } from '../service/model/Book';
import 'rxjs/Rx';
import { BookService } from '../service/api/book.service';
@Component({
selector: 'bs-book-detail',
templateUrl: './book-detail.component.html',
styles: []
})
export class BookDetailComponent implements OnInit {
private book: Book = new Book();
constructor(private router: Router, private route: ActivatedRoute, private bookService: BookService) { }
ngOnInit() {
this.route.params
.map(params => params['bookId'])
.switchMap(id => this.bookService.getBook(id))
.subscribe(book => this.book = book);
}
delete() {
this.bookService.deleteBook(this.book.id)
.finally(() => this.router.navigate(['/book-list']))
.subscribe();
}
}
书店前台/src/app/service/model/Book
/**
* BookStore APIs
* BookStore APIs exposed from a Java EE back-end to an Angular front-end
*
* OpenAPI spec version: 1.0.0
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
/**
* Book resourse representation.
*/
export interface Book {
/**
* Identifier
*/
id?: number;
/**
* Title of the book
*/
title: string;
/**
* Summary describing the book
*/
description?: string;
/**
* Unit cost
*/
unitCost?: number;
/**
* ISBN number
*/
isbn: string;
/**
* Date in which the book has been published
*/
publicationDate?: Date;
/**
* Number of pages
*/
nbOfPages?: number;
/**
* URL of the image cover
*/
imageURL?: string;
/**
* Language in which the book has been written
*/
language?: Book.LanguageEnum;
}
export namespace Book {
export type LanguageEnum = 'ENGLISH' | 'FRENCH' | 'SPANISH' | 'PORTUGUESE' | 'ITALIAN' | 'FINISH' | 'GERMAN' | 'DEUTSCH' | 'RUSSIAN';
export const LanguageEnum = {
ENGLISH: 'ENGLISH' as LanguageEnum,
FRENCH: 'FRENCH' as LanguageEnum,
SPANISH: 'SPANISH' as LanguageEnum,
PORTUGUESE: 'PORTUGUESE' as LanguageEnum,
ITALIAN: 'ITALIAN' as LanguageEnum,
FINISH: 'FINISH' as LanguageEnum,
GERMAN: 'GERMAN' as LanguageEnum,
DEUTSCH: 'DEUTSCH' as LanguageEnum,
RUSSIAN: 'RUSSIAN' as LanguageEnum
}
}
如果我尝试更改私人书籍:Book = new Book();
至
私人书籍:书籍;
我收到这个错误
BookFormComponent.html:6 ERROR TypeError: Cannot read property 'title' of undefined
at Object.eval (BookFormComponent.html:6)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:14697)
at checkAndUpdateView (core.js:13844)
at callViewAction (core.js:14195)
at execComponentViewsAction (core.js:14127)
at checkAndUpdateView (core.js:13850)
at callViewAction (core.js:14195)
at execEmbeddedViewsAction (core.js:14153)
at checkAndUpdateView (core.js:13845)
at callViewAction (core.js:14195)
是如果您需要/希望创建一个可能是自定义对象的实例,同时获得类型检查的好处,例如参数、返回类型或泛型 - 一个类是有意义的。如果您不创建实例 - 我们有接口可供我们使用,它们的好处来自不生成任何源代码,但允许我们在某种程度上“虚拟”类型检查我们的代码。是的教程在 angular 中使用 javaEE 标准,但它是有点不同,你可以这样写你的界面:
interface Book {
id?: number;
title: string;
description?: string;
unitCost?: number;
isbn: string;
publicationDate?: Date;
nbOfPages?: number;
imageURL?: string;
language?: Book.LanguageEnum;
}
type LanguageEnum = 'ENGLISH' | 'FRENCH' | 'SPANISH' | 'PORTUGUESE' | 'ITALIAN' | 'FINISH' | 'GERMAN' | 'DEUTSCH' | 'RUSSIAN';
const LanguageEnum = {
ENGLISH: 'ENGLISH' as LanguageEnum,
FRENCH: 'FRENCH' as LanguageEnum,
SPANISH: 'SPANISH' as LanguageEnum,
PORTUGUESE: 'PORTUGUESE' as LanguageEnum,
ITALIAN: 'ITALIAN' as LanguageEnum,
FINISH: 'FINISH' as LanguageEnum,
GERMAN: 'GERMAN' as LanguageEnum,
DEUTSCH: 'DEUTSCH' as LanguageEnum,
RUSSIAN: 'RUSSIAN' as LanguageEnum
}
export{
Book,
LanguageEnum,
LanguageEnum
}
导入您需要的每个人并使用它们,以使用界面:
import { Book } from '../service/model/Book';
private book: Book = {title: 'title' , isbn: 'isbn' , ... }
并且无需将其转换为类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句