Aurelia CLI & TypeScript & MomentJS

前端 未结 2 1724
一整个雨季
一整个雨季 2021-01-14 04:11

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.

2条回答
  •  天涯浪人
    2021-01-14 04:34

    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();
        }
    }
    

提交回复
热议问题