Typescript 基于方法可用性创建RxJs observable
在Angular 4应用程序中使用RxJs observable和TypeScript执行之前,我想等待方法可用性 到目前为止,我提出了使用Typescript 基于方法可用性创建RxJs observable,typescript,rxjs,observable,Typescript,Rxjs,Observable,在Angular 4应用程序中使用RxJs observable和TypeScript执行之前,我想等待方法可用性 到目前为止,我提出了使用interval与skipWhile相结合的方法,但我想知道是否有更好的方法来“观察”可用的方法,而不是每100毫秒观察一次 可观察 .间隔(100) .skipWhile(()=>myObject.myFunction==null) .first() .subscribe(()=>myObject.myFunction()); 我是一个新的观察者,所以任
interval
与skipWhile
相结合的方法,但我想知道是否有更好的方法来“观察”可用的方法,而不是每100毫秒观察一次
可观察
.间隔(100)
.skipWhile(()=>myObject.myFunction==null)
.first()
.subscribe(()=>myObject.myFunction());
我是一个新的观察者,所以任何洞察都是受欢迎的
//创建不带sayHello函数的元素
常量myElement={};
//在显示警报的1000毫秒后添加方法sayHello
setTimeout(()=>myElement.sayHello=()=>alert('Hello'),1000);
//如果sayHello函数在执行之前存在,则每100毫秒观察一次
可观测
.间隔(100)
.skipWhile((间隔)=>{
const isUndefined=myElement.sayHello==null;
写入(间隔,未定义);
返回未定义;
})
.first()
.subscribe(()=>myElement.sayHello());
//函数将结果写入DOM
常量写入=(间隔,未定义)=>{
const节点=document.createElement(“DIV”);
const textnode=document.createTextNode('interval#'+interval.toString()+'-sayHello是'+(isUndefined?'UNDEFINED':'DEFINED');
node.appendChild(textnode);
document.body.appendChild(节点);
};代码>
如果你需要投票,你不能做得更好。正确的反应方式应该是myObject
在方法准备就绪并且您订阅它时发出事件