【发布时间】:2018-03-21 05:31:33
【问题描述】:
In this Plunk,我对 Angular.io 教程做了一些小修改。
我在 heros.service 中添加了一个名为 doHeroesExist 的函数。
但是,我从不调用它。我唯一做的就是将它分配给app.component中的一个变量
ngOnInit(): void {
//this.getHeroes();
this.heroesExist = this.heroService.doHeroesExist;
console.log("app ngOnInit called...", this.heroesExist); // outputs: app ngOnInit called... true
}
这是hero.service.ts 文件中的doHeroesExist 函数。
doHeroesExist(): boolean {
console.log("doHeroesExist called..", this.doHeroesExist);
return this.doHeroesExist;
}
我很困惑。
为什么控制台日志中显示 true?它不应该将函数的主体输出为字符串吗? doHeroesExist 中的 console.log 也从未打印过 - 进一步证明它没有被调用!
【问题讨论】:
标签: angular this angular-services angular-components