请指点我。我知道snap.load()是一个异步调用,但我想在组件构造函数内部同步,这样在构造完成时我已经准备好使用所有svg元素,或者将元素分配给子组件。像这样的东西。所以基本上,如何在snap.load完成之前阻止构造函数?
import { Component } from '@angular/core';
import { ChildComponent } from 'child.component';
@Component({
selector: 'parent',
directives: [ChildComponent]
})
export class AppComponent {
svgPath: string = "assets/svg/sprites.svg"
SVGElements: any;
constructor(){
var self = this;
Snap.load(svgPath, function(items) {
items.selectAll().forEach(function(item) {
var clone = item.clone()
self.SVGElements.push(clone);
});
}
//this show empty, I know because snap.load is an async call
console.log(this.SVGElements);
}
ngOnInit() {
//I want to do something with this.SVGElements here
}
}