2017-09-16 21 views
-1

下面是代码,我想实现:如何将接口作为参数传递并在TypeScript中获取匿名回调?

this._customHttp.httpPostService('app/fetchGridDetails',this.req,new 
    InterfaceName() { 
     onEvent(str :string) { 
      console.log(str); 
     } 
    }); 
+1

能“没有运气”可以在具体的错误信息表达? –

+0

你想完成什么?传递的接口应该达到什么目的? –

+1

https://stackoverflow.com/questions/41623998/typescript-optional-callback-parameter-does-not-match-anonymous-function-passed? – Walfrat

回答

3

如果它是一个接口,而不是一类,你不new起来。您可以创建一个实现内联接口的对象:

this._customHttp.httpPostService('app/fetchGridDetails', this.req, 
    <InterfaceName>{ 
     onEvent(str: string) { 
      console.log(str); 
     } 
    }); 

您可能甚至不需要明确写入接口名称。如果对象具有由httpPostService预计它的正确方法,打字稿应该编译:

this._customHttp.httpPostService('app/fetchGridDetails', this.req, 
    { 
     onEvent(str: string) { 
      console.log(str); 
     } 
    }); 
+0

非常感谢你,它完美的工作,上帝保佑你 –

相关问题