I\'m not getting Aurelia (CLI) & TypeScript & MomentJS to work together. I\'ve seen solutions for Aurelia & Moment problems but they don\'t use the Aurelia CLI.
Update
This doesn't actually work. Not the thing compiles but I'm getting "TypeError: Cannot read property 'format' of undefined" when running the app.
Original
Ah, stupid me. I just had to inject Moment into the class. Obviously.
So here's the working version:
import { Moment } from 'moment';
import { autoinject } from "aurelia-framework";
export class App {
message = 'Hello World!';
moment: Moment;
constructor(moment : Moment){
this.moment = moment;
}
hello() : string {
return this.moment.format();
}
}
MomentJS is a global module, which exports a moment
variable only.
Other interface definitions in the d.ts file, e.g. interface Moment
, won't be exported for real. Those are there for the TypeScript compiler and intellisense.
That's why you've experienced TS compiler and TypeError issues above. You may've tricked the compiler with autoinject but not the browser.
moment's definition file:
declare module 'moment' {
var moment: moment.MomentStatic;
export = moment;
}
In order to get it working, use an import statement like this: [moment docs]
import * as moment from 'moment';
After that, the moment
variable becomes available and you can use it as you normally would.
Usage in your class:
import * as moment from 'moment';
export class App {
message = 'Hello World!';
constructor(){
}
hello() : string {
return moment().format();
}
}