简体   繁体   English

类型 Class 和组件 angular 上不存在属性

[英]Property does not exist on type Class and components angular

Im building simple angular application.我正在构建简单的 angular 应用程序。 and i get error on component(book-add) html.我在组件(书籍添加)html 上遇到错误。

 <div class="form-group">
    <label for="title"><span class="req">* </span> Title:</label>
    <input required type="text" [(ngModel)]="book.title" name="title" id="title"
    class="form-control phone" maxlength="28" placeholder="Enter book title..." />
                    </div>

Property 'title' does not exist on type 'Book'. “书”类型上不存在属性“标题”。

in book-add.component.ts i intialized Book class and by default it is showing i can enter title and author so that means it recognized Book class在 book-add.component.ts 我初始化了 Book class 并且默认情况下它显示我可以输入标题和作者,这意味着它识别了 Book class

book: Book=new Book("","");

this is class book.ts这是 class book.ts

export class Book{
    constructor(
        title:String,
        author:String
    ){}
}

Why i am getting this error为什么我收到此错误

You are missing the access modifier which turns it into a field on the type.您缺少将其转换为类型字段的访问修饰符。 constructor( public title: string, public author:string){} . constructor( public title: string, public author:string){} Without that you are just passing in arguments that the code is completely ignoring.没有它,您只是传入代码完全忽略的 arguments。

export class Book{
    constructor(
        public title:String,
        public author:String
    ){}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM