2017-04-07 5 views
1

Ich arbeite an der Verwendung moment.js with typescript. Ich lief die folgenden BefehleIonic 2 Moment JS mit Moment TimeZones

npm install moment-timezone --save 
npm install @types/moment @types/moment-timezone --save 

aber aus irgendeinem Grund in der formattime Funktion, ist zur Zeit nicht definiert. Ich bekomme keine Fehler von meiner IDE in Bezug auf meinen Import Moment und ich kann sehen, moment und moment-timezone sind in der npm Ordner. Ich bin durch den Post gekrochen, aber ich sehe nicht viele hilfreiche Informationen. Jede Hilfe wäre sehr sein geschätzt

import moment from 'moment'; 

export class Utils { 

    text: string; 

    constructor() { 
     console.log('Hello Utils Component'); 
    } 


    //This function take the time in MS and the timezone and coverts it 
    //@params time - time in milliseconds 
    //@params tz - the timezone 
    //@paramz isZoneAbbr - true if the zone abbr should be displayed 
    //@return - return formattedTime 
    public formatTime(time, tz, isZoneAbbr): string { 
     console.log(JSON.stringify(moment)); 
     return ""; 
     // var formattedTime = moment.tz(time, tz); 
     // if (isZoneAbbr) { 
     //  return formattedTime.format('h:mm a z'); 
     // } 
     // return formattedTime.format('h:mm a'); 
    }; 
} 
+0

ich meine Import-Anweisung, da beide 'Import versucht haben, * als Moment aus 'Moment'; '&' Moment aus 'Moment' importieren; ' – VirtualProdigy

+0

brauchst du [angular2-moment] (https://github.com/urish/angular2-moment) auch –

+0

@gerdi kein angular-moment ist nur Pipes meist. Sie sollten den tatsächlichen Moment in npm installieren, da Sie nur moment-timezones & types installiert haben – misha130

Antwort

2

Sie wahrscheinlich als moment-timezone und nicht moment verweisen, weil es nicht das moment Paket ist

import * as momentTz from 'moment-timezone'; 
+0

Ich habe das auch versucht, aber ich bekomme immer noch undefined – VirtualProdigy

+0

Was ist Moment Zeitzone in Ihrer 'package.json' Datei genannt? –

+0

Ich habe '" moment ":"^2.18.1 "' '" moment-timezone ":"^0.5.13 "' Ich fügte auch '' @ types/moment-timezone "hinzu:"^0.2.34 "' – VirtualProdigy