2016-09-04 9 views
1

プロジェクトにdebounceライブラリproperliをインポートする際に問題があります。reactJs + typescript + debounce - 正しくデバウンスをインポートできません。

は、私が行っている:私のファイルに

  1. npm install debounce --save
  2. typings install dt~debounce --save --global
  3. 、輸入デバウンスとして:import debounce from 'debounce';
  4. 使用デバウンスのように:これらのコードに私のコードではdebounce(function(){ console.log('testtt'); })();

コンパイルは合格ですが、私が実行しようとしているときbsideは、私はエラーを取得:

Uncaught TypeError: debounce_1.default is not a function

私は間違って何をしているのですか?

おかげ

答えて

1

私はlodashライブラリはdebounce機能とtypescriptですためのタイピングを持っていることを考え出しました。

それはこのように動作します:私はちょうど同様に、この問題に遭遇した

import _ = require('lodash'); 

... 

class SearchControl extends React.Component<ISearchControlProps, ISearchControlState> { 
    private debouncedLoadSuggestions; 
    constructor(props: any) { 
     ... 
     this.debouncedLoadSuggestions = _.debounce(this.fetchSuggestions, 500); 
    } 

    fetchSuggestions = (value: string) => { 
     ... 
    }; 

    onSuggestionsFetchRequested = ({value}) => { 
     this.debouncedLoadSuggestions(value); 
    }; 
} 
0

debounceの入力はちょうど間違っているようです。おそらく旧バージョンのライブラリの非モジュール版です。私は将来的に誰のために、これらのタイピングは私のために働くので、ちょうどこの機能のためにlodashのすべてに持参するように傾斜していないよ:

declare module "debounce" { 
    function debounce<F extends Function>(func: F, wait?: number, immediate?: boolean): F; 
    namespace debounce {} 
    export = debounce; 
} 

例:

import * as debounce from "debounce"; 

document.body.addEventListener("mousemove", debounce((e: MouseEvent) => console.log("moved"), 500)); 
関連する問題