简体   繁体   中英

Moment.js plugins in Ionic2/Cordova typescript project

In my Ionic 2 project written in Typescript I use moment.js library. I import it using code snippet:

import * as moment from 'moment';

After I do so, in my component I can use moment normally, ie:

let endDate = moment(data.endDate);

however, now I want to use one of plugins dedicated to moment.js - moment-weekday-calc from this repo: https://github.com/andruhon/moment-weekday-calc

I've installed plugin via npm, but I am not able to make it running. I've tried:

import * as moment from 'moment';
import 'moment-weekday-calc';

//(...) - my component's code here

  let test = moment().isoWeekdayCalc({
    rangeStart: '1 Apr 2015',
    rangeEnd: '31 Mar 2016',
    weekdays: [1,2,3,4,5],
    exclusions: ['6 Apr 2015','7 Apr 2015'],
    inclusions: ['10 Apr 2015']
  }); //260

above code throws an error:

Typescript Error
Property 'isoWeekdayCalc' does not exist on type 'Moment'.

any idea how can I use this plugin in my typescript app for ionic/cordova?

Since that error is just typescript complaining about isoWeekdayCalc not being defined in the type definition of moment , you can just cast it to any like this:

let test = (<any>moment()).isoWeekdayCalc({
            rangeStart: '1 Apr 2015',
            rangeEnd: '31 Mar 2016',
            weekdays: [1,2,3,4,5],
            exclusions: ['6 Apr 2015','7 Apr 2015'],
            inclusions: ['10 Apr 2015']
        }); //260

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM