admin管理员组文章数量:1401632
I am trying to use the element-resize-detector library () in an Angular2 application.
From my limited JS module knowledge, the library appears to be in CommonJS format. After several attempts, I have created the following definition file (*.d.ts):
declare module ResizeDetector {
function ResizeDetector(options: any): ResizeDetector.Erd;
interface Erd {
listenTo(element: any, resizeCallback: any): void;
uninstall(element: any): void;
}
}
export = ResizeDetector;
I then use the following import statement in my TypeScript code:
import * as ResizeDetector from 'element-resize-detector';
When running my app and using console.log('ResizeDetector', ResizeDetector)
, the following output is logged:
ResizeDetector function (options) {
options = options || {};
//idHandler is currently not an option to the listenTo function, so it should not be added to globalOptions.
var idHandler;
if (options.…
This shows me that the library is successfully loaded and that it returns a function as expected.
My question is how can I now start using the library in TypeScript? When I attempt something like:
private static resizeDetector = ResizeDetector({ strategy: 'scroll' });
I get the following transpile error:
error TS2349: Cannot invoke an expression whose type lacks a call signature.
I am trying to use the element-resize-detector library (https://github./wnr/element-resize-detector) in an Angular2 application.
From my limited JS module knowledge, the library appears to be in CommonJS format. After several attempts, I have created the following definition file (*.d.ts):
declare module ResizeDetector {
function ResizeDetector(options: any): ResizeDetector.Erd;
interface Erd {
listenTo(element: any, resizeCallback: any): void;
uninstall(element: any): void;
}
}
export = ResizeDetector;
I then use the following import statement in my TypeScript code:
import * as ResizeDetector from 'element-resize-detector';
When running my app and using console.log('ResizeDetector', ResizeDetector)
, the following output is logged:
ResizeDetector function (options) {
options = options || {};
//idHandler is currently not an option to the listenTo function, so it should not be added to globalOptions.
var idHandler;
if (options.…
This shows me that the library is successfully loaded and that it returns a function as expected.
My question is how can I now start using the library in TypeScript? When I attempt something like:
private static resizeDetector = ResizeDetector({ strategy: 'scroll' });
I get the following transpile error:
error TS2349: Cannot invoke an expression whose type lacks a call signature.
Share
Improve this question
asked Dec 7, 2016 at 21:31
Michael McGowanMichael McGowan
5661 gold badge5 silver badges13 bronze badges
2 Answers
Reset to default 5Based on @stone-shi answer this is how I got it to work:
Definition file:
declare module 'element-resize-detector' {
function elementResizeDetectorMaker(options?: elementResizeDetectorMaker.ErdmOptions): elementResizeDetectorMaker.Erd;
namespace elementResizeDetectorMaker {
interface ErdmOptions {
strategy?: 'scroll' | 'object';
}
interface Erd {
listenTo(element: HTMLElement, callback: (elem: HTMLElement) => void): void;
removeListener(element: HTMLElement, callback: (elem: HTMLElement) => void): void;
removeAllListeners(element: HTMLElement): void;
uninstall(element: HTMLElement): void;
}
}
export = elementResizeDetectorMaker;
}
Ts file
import * as elementResizeDetectorMaker from 'element-resize-detector';
let _elementResizeDetector = elementResizeDetectorMaker({
strategy: 'scroll'
});
element-resize-detector.d.ts
declare function elementResizeDetectorMaker(options?: elementResizeDetectorMaker.ErdmOptions): elementResizeDetectorMaker.Erd;
declare namespace elementResizeDetectorMaker {
interface ErdmOptions {
strategy?: 'scroll' | 'object';
}
interface Erd {
listenTo(element: HTMLElement, callback: (elem: HTMLElement) => void);
removeListener(element: HTMLElement, callback: (elem: HTMLElement) => void);
removeAllListeners(element: HTMLElement);
uninstall(element: HTMLElement);
}
}
export = elementResizeDetectorMaker;
Consumer
let resizeDetector = elementResizeDetectorMaker({ strategy: 'scroll'});
resizeDetector.listenTo(document.getElementById('abc'), (elem: HTMLElement) => {
console.log(elem.offsetWidth, elem.offsetHeight);
})
本文标签: javascriptUse elementresizedetector library in an Angular2 applicationStack Overflow
版权声明:本文标题:javascript - Use element-resize-detector library in an Angular2 application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744279011a2598560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论