一个月前,我发布了一个小的library,它只是允许显示一个加载动画。角4事件发射器没有捕捉任何订阅
为此,我的用户只需将<ng-load></ng-load>
添加到他们的模板中,并在其组件中注入LoadService
并呼叫loadService.animate(true)
(或false)。
对事物的库边我做到以下几点:
在我的服务:
@Injectable()
export class LoadService {
// My event emitter
@Output() animating: EventEmitter<any> = new EventEmitter();
constructor() {}
// Method called by the user to display the animation,
// emits the value passed as parameter
public animate(val: boolean) {
this.animating.emit(val);
}
getValue(): any {
return this.animating;
}
}
而且在我的组件:
@Component({
selector: 'ng-load',
templateUrl: './load.component.html',
styleUrls: ['./load.component.css']
})
export class LoadComponent implements OnInit {
animate: boolean;
constructor(public loadService: LoadService) {}
ngOnInit(): void {
// Listen to our event emitter
this.loadService.getValue().subscribe((status: boolean) => {
this.animate = status;
});
}
}
我HTML
模板只是有一个<div *ngIf="animate">
到显示或隐藏CSS
动画。
所以我的事件发射器发射什么我的用户给予animate
,和我的布尔值应该在我的成分发生变化,或因此它做了一个月前。
由于某种原因,我的事件发射器订阅没有任何东西。
是否已更改EventEmitters
?我是否以错误的方式使用它们?
这就是我认为,在阅读了关于EventEmitters的详细信息之后,我现在意识到我的实现可能会在未来(或者现在,就此而言)导致问题。 对我而言,最好的方法是什么?创建一个我的组件将订阅的“Observable”? –
Christopher
在getValue()中,尝试返回Observable.of(this.animating)并从代码中删除@Output。 –