我无法从ngx-countdown中获取私有的'left'属性。
我已经读过你应该将该属性声明为public,但这只是我安装的库(ngx-countdown)
我得到的错误:
错误在src / app / pages / exam-levels / exam-level2 / exam-level2.component.ts(129,30):错误TS2341:属性'left'是私有的,只能在类'CountdownComponent'中访问。 src / app / pages / exam-levels / exam-level2 / exam-level2.component.ts(130,39):错误TS2341:属性'left'是私有的,只能在'CountdownComponent'类中访问。
TS文件
import { Component, OnInit, ViewChild } from '@angular/core';
import { Subscription } from 'rxjs';
import { Router } from '@angular/router';
import { ExamsService } from '@app/services/exams.service';
import { ExamsModel } from '@app/models/exams.model';
import { HostListener } from '@angular/core';
import { ConfirmationModalModel } from '@app/models/confirmation-modal.model';
import { CountdownComponent } from 'ngx-countdown';
@Component({
selector: 'app-exam-level2',
templateUrl: './exam-level2.component.html',
styleUrls: ['./exam-level2.component.scss']
})
export class ExamLevel2Component implements OnInit {
remainingTime: any;
seconds = CountdownComponent['left'];
public questionsListLevel2: Array<ExamsModel>;
public examLevel2ModalContent: ConfirmationModalModel;
public timeLeft: CountdownComponent["left"];
level2QuestionsSet$: Subscription;
pageTab: string;
isToggled: boolean;
status = '';
@ViewChild('countdown') counter: CountdownComponent;
resetTimer() {
this.counter.restart();
this.counter.stop();
this.counter.pause();
this.counter.resume();
}
constructor(
private router: Router,
private examsService: ExamsService,
) {
this.pageTab = "exam-level2";
}
ngOnInit() {
this.getQuestionsListPart2();
this.isToggled = false;
}
// TIMER
finishTest() {
console.log('count down', this.counter);
setTimeout(() => {
this.router.navigate(['/exam-level3']);
}
, 3000);
}
stopTimer () {
this.counter.pause();
console.log(this.counter);
// console.log(this.counter.finished)
// this.timerService.sayHello();
}
onNotify() {
console.log(this.counter.left);
this.remainingTime = this.counter.left;
this.store();
}
store(){
let key = 'Timer';
localStorage.setItem(key, this.remainingTime);
}
}
模板
<!-- TIMER START -->
<div class="timer-container">
<countdown #countdown [config]="{leftTime: 1800, notify: [ 1795 ]}" (finished)="finishTest()" (notify)="onNotify($event)">$!m!:$!s!</countdown>
<button (click)="stopTimer()" class="btn btn-link btn-sm">STOP</button>
<button (click)="store()" class="btn btn-link btn-sm">STORE</button>
</div>
<!-- TIMER END -->
在我的情况下,你可以通过活动访问。
HTML:
<countdown [config]="config" (event)="onEvent($event)">
打字稿:
onEvent($event): void {
let timeLeft = $event.left;
}
对于notify事件,它必须是相同的。通过这种方式,您可以无私错误地访问