8000 第一章节:1.9使用数据模型,article.component初始化加载异常 · Issue #23 · ng-book2/book · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
第一章节:1.9使用数据模型,article.component初始化加载异常 #23
Open
@wwx362192

Description

@wwx362192

在1.9小节-88页,书中直接在constructor中初始化this.article,导致在加载页面的时候,this.article找不到,undefined,页面展示异常
应该把初始化操作this.article赋值操作放于ngOnInit中操作,constructor意为在类生成实例时调用,适用于依赖注入,所注入的可作为类的属性来使用。而ngOnInit则是angular规定的初始化操作的地方。

code/first_app/angular2_reddit/src/app/article/article.component.ts
export class ArticleComponent implements OnInit {
article: Article;
constructor() {
this.article = new Article(
'Angular',
'http://angular.io',
10);
}
voteUp(): boolean {
this.article.voteUp();
return false;
}
voteDown(): boolean {
this.article.voteDown();
return false;
}
ngOnInit() {
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0