【发布时间】:2019-12-19 18:18:30
【问题描述】:
我开发了一项服务,允许我激活按钮并查看其在不同组件中的状态。 当我点击开始/暂停时,按钮会改变图像。
在 StackBlitz 中,代码完美运行,但是当我在我的项目中实现它时,该函数只在 ngOnInit 中传递一次并且不再加载,不再更改组件之间的按钮图像。
有没有办法实现这个但不使用ngOnInit?
我使用了setinterval,按钮改变了图像,但这似乎不是最好的解决方案。
谁能帮帮我?
问题---关于组件
currentState: string;
ngOnInit() {
this.currentState = this.servicesService.getCurrentState();
}
html
<div class="container" style="margin-top: 10%">
<div class="btn-group" dropdown>
<button id="button-basic" dropdownToggle type="button" class="btn ">
<img style="width: 35px;" *ngIf="currentState=='pause'" src="https://img.icons8.com/carbon-copy/100/000000/play-button-circled.png">
<img style="width: 35px;" *ngIf="currentState=='start'" src="https://img.icons8.com/cute-clipart/64/000000/stop-squared.png">
</button>
<ul id="dropdown-basic" *dropdownMenu class="dropdown-menu" role="menu" aria-labelledby="button-basic">
<li role="menuitem">
<a class="dropdown-item" *ngIf="currentState=='pause'" routerLinkActive="active" (click)="startTimer()">Start</a>
</li>
<li role="menuitem">
<a class="dropdown-item" *ngIf="currentState=='start'" routerLinkActive="active" (click)="pauseTimer()">Stop</a>
</li>
</ul>
<div>
<span>{{servicesService.fetchDisplay()}}</span>
</div>
</div>
</div>
组件
startTimer() {
this.servicesService.startTimer();
this.currentState = this.servicesService.getCurrentState();
}
pauseTimer() {
this.servicesService.pauseTimer();
this.currentState = this.servicesService.getCurrentState();
}
【问题讨论】:
-
ngOnInit 在组件生命周期中只运行一次。如果您不想多次运行某些内容,则需要使用 setInterval 等。如果您需要运行一次,但在视图启动后,您可以尝试 ngAfterViewInit
-
您好!您实际上需要在不同的组件中使用状态,我不会回答您的问题,但我希望您看看 blog.angular-university.io/angular-2-redux-ngrx-rxjs 。如果您计划在多个组件中使用不同的状态,NGRX 可能是一个不错的选择 :) 如果您不了解 NGRX,您应该阅读一个博客,如果您仍想使用此服务模式,我建议您获取当前的*ngIf 中的状态如下: *ngIf="this.servicesService.getCurrentState() === 'pause'"
标签: angular typescript