我主要担心的是,$event
显示该行错误:
starClick($event) {
参数$事件隐含有 '任意' 类型
我也怀疑 - 根据Angular2文档,$event
所做的是捕获事件对象,因此让我问一个愚蠢的问题 - 为什么我们不称它为$object
?因为它让我迷惑了不少,直到我终于意识到发生了什么这里..
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'stars',
template: `
<span class="glyphicon glyphicon-star-empty" (click)= "starClick($event)"></span>
`
})
export class StarsComponent {
starClick($event) {
if($event.target.className == "glyphicon glyphicon-star-empty") {
$event.target.className = "glyphicon glyphicon-star";
}
else{
$event.target.className = "glyphicon glyphicon-star-empty";
}
}
}
'$ event'在你的案例中有MouseEvent类型,你可以在你的方法中添加这个以避免警告。 – hiper2d
从EventEmitter传递'$ event'。如果它是ObjectEmitter,它将是'$ object' - 但事实并非如此。 – estus