我有以下组件在onClick事件中我试图递增/递减其count字段的值。但是,当我这样做时,它将计数字段转换为字符串。我理解插值将所有内容转换为字符串但不应该将字符串转换为正确的类型,因为我使用字段吗?我在这里错过了什么吗?
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'like',
template: `<i class="glyphicon glyphicon-heart" [class.liked]="isLiked" (click)="onClick()"></i><span>{{count}}</span>`,
styleUrls: ['./like.component.css']
})
export class LikeComponent implements OnInit {
@Input() isLiked: boolean;
@Input() count: number;
@Output() change = new EventEmitter();
constructor() { }
onClick() {
console.log(typeof this.count); // <--- string
this.isLiked = !this.isLiked;
this.count = Number(this.count) + (this.isLiked ? 1 : -1);
this.change.emit({value: this.isLiked});
}
ngOnInit() {
}
}
答案 0 :(得分:2)
<like count="10"></like>
就像
<like [count]="'10'"></like>
所以
<like [count]="10"></like>