下面是一个使用Angular编写的计时器组件的示例代码:
ng generate component Timer
timer.component.ts
文件中,编写以下代码:import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-timer',
templateUrl: './timer.component.html',
styleUrls: ['./timer.component.css']
})
export class TimerComponent implements OnInit {
currentTime: number = 0;
interval: any;
constructor() { }
ngOnInit() {
this.startTimer();
}
startTimer() {
this.interval = setInterval(() => {
this.currentTime++;
}, 1000);
}
pauseTimer() {
clearInterval(this.interval);
}
resetTimer() {
this.currentTime = 0;
}
}
timer.component.html
文件中,编写以下代码:
Timer: {{ currentTime }} seconds
这个示例中的计时器会每秒钟增加1秒,并且提供了暂停和重置功能。你可以根据自己的需求对计时器进行修改和扩展。