2017-02-17 9 views
0

私は下に次のコードしている:Javascriptでは、クラスまたはオブジェクトではなく、関数だけをインポートする方法は?

import { Router } from 'express' 
. 
. 
. 
const router = Router() 

以下のコードは

router.post('/authenticate', AuthController.authenticate) 

として使用することができますどのように:

post('/authenticate', AuthController.authenticate)

にはどうすれば機能のみをインポートしていませんクラス、型、またはオブジェクト?

+0

は、mehod https://github.com/expressjs/express/blob/master/lib/router/route.jsではなく、Express Routerのみをエクスポートします。 const post = Router()。post –

答えて

0
const post = Router().post 

その後、post('/authenticate', AuthController.authenticate)を使用できるはずです。

+2

あなたは 'const r = Router()、post = r.post.bind(r)'をしなければならないかもしれません。この特定のケースでこれが必要かどうかはわかりませんが、オブジェクトがどのように実装されているかによって、これが必要になる場合があります。 – deceze

0

パッケージ全体をインポートする必要があります。

import { Router } from 'express' 
. 
. 
. 
const post = Router().post 

上記のスニペットはファイル全体をインポートしますが、特定の関数を変数に割り当てるだけです。

関連する問題