2016年4月26日火曜日

ionic2でunderscorejsを使用する

ionic2でTypeSciprtで開発している場合、underscorejs[lodash]をただ使おうとすると、
Cannot find name '_' 
と出ると思います。

事前準備

事前にtypingsを入れておく必要があります。
npm install -g typings

underscorejsをいれる

npm install underscore --save

underscorejsの型定義ファイル作成

typings install underscore --ambient --save

typings/browser/ambient/underscore/index.d.tsが作成されます

tsconfig.json設定 

filesへmain.d.tsを追加
  "files": [
      "typings/main.d.ts"
  ],


undescorejs使用

使用したいファイルで読み込みます。

import * as _ from 'underscore';

上記だと全ての関数が読み込まれますが



import "reject" from 'underscore';
のようにして個別にも読み込めるようです。

0 件のコメント:

コメントを投稿