2016-07-13 2 views
3

moment2j을 angular2 응용 프로그램으로 가져 오려고합니다. 우선, 나는이 guide을 따라 갔고 나는이 answer을 발견했다. 그러나 제안 된 솔루션을 따라하려고했지만 여전히 moment.js를 가져올 수 없다. 패키지가 있고 실제로 IDE (visual studio)가 문제없이 moment.d.ts 파일을 찾을 수 있지만 npm start로 응용 프로그램을 실행할 때 콘솔에 다음 두 가지 오류가 표시됩니다.moment.js angular2를 가져 오는 방법

"NetworkError: 404 Not Found - http://localhost:3000/node_modules/moment/ " /node_m...moment/ Error: patchProperty/desc.set/[email protected]http://localhost:3000/node_modules/zone.js/dist/zone.js:769:27 Zonehttp://localhost:3000/node_modules/zone.js/dist/zone.js:356:24 Zonehttp://localhost:3000/node_modules/zone.js/dist/zone.js:256:29 ZoneTask/[email protected]http://localhost:3000/node_modules/zone.js/dist/zone.js:423:29
Error loading http://localhost:3000/node_modules/moment as "moment" from 'my file'

나는 여전히

import * as moment from 'moment/moment.d' 

아무것도하지만,이 방법

import * as moment from 'moment' 

에서이 방법으로 순간을 가져올 수있어 시도 오류가 발생했습니다. 그래서 그것을 제거하고 명령을 다시 입력했다

Typings for "moment" already exist in "node_modules/moment/moment.d.ts". You should let TypeScript resolve the packaged typings and uninstall the copy installed by Typings

잘 나는이 오류를 얻을 : 내 SystemJs'map 속성은 하나

var map = { 
    'app':      'app', // 'dist', 
    '@angular':     'node_modules/@angular', 
    'services':     'app/service', 
    'moment':      'node_modules/moment',//moment.js 
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api', 
    'rxjs':      'node_modules/rxjs' 
}; 

나는이 오류가 typings에 설치하려고하면 이것이다

typings ERR! message Unable to find "moment" ("npm") in the registry.

그래서이 딜레마에서 벗어나는 방법은 무엇입니까?

+0

[2 각 타입 스크립트에서 moment.js 라이브러리 사용 방법] (http://stackoverflow.com/questions/35166168/how-to-use-moment-js-library-in-angular-2 -typeescript-app) –

+1

이 답변에는 systemjs config에 대한 설명도 있습니다. 그리고 문제는 그것을 어떻게 사용하는 것이 아니라 그것을 어떻게 가져올 것인가입니다. – mautrok

답변

5

첫째, NPM을 통해 순간 패키지를 설치 :

npm install moment --save 

을 두 번째로 당신의 systemjs 설정 (2 라인)을 변경 :

(function (global) { 
    // map tells the System loader where to look for things 
    var map = { 
    'app': 'app', // 'dist', 
    '@angular': 'node_modules/@angular', 
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api', 
    'rxjs': 'node_modules/rxjs', 
    'moment': 'node_modules/moment', <== add this line 
    }; 
    // packages tells the System loader how to load when no filename and/or no extension 
    var packages = { 
    'app': { main: 'main.js', defaultExtension: 'js' }, 
    'rxjs': { defaultExtension: 'js' }, 
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' }, 
    'moment': { main: 'moment.js', defaultExtension: 'js' } <== add this line 
    }; 

그런 다음 당신은 그것을 사용할 수 있습니다

import * as moment from 'moment'; 

입력은 node_modules/moment/moment.d.ts

에서 사용할 수 있습니다.