简体   繁体   中英

Function result undefined when using RequireJS with exported class

I'm using Typescript, Require, and AMD modules. When I call this function, it returns undefined. If I log the values inside the function, they are all coming back correctly.

If I don't use require([ in the exported class, it won't compile.

Exported class from common.ts file.

export class DateHelpers {
    CheckMaxDateRange(fromDate: string, toDate: string, numberOfDays: Number) {
        requirejs(["moment", "alertify"], function (moment, alertify) {
            moment.locale('en');
            var momToDate = moment(toDate, "MM/DD/YYYY");
            var momFromDate = moment(fromDate, "MM/DD/YYYY");

            var dateDiff = momToDate.diff(momFromDate, 'days');

            if (dateDiff < 0) {
                alertify.alert("From Date must be before To Date.");
                return false;
            }

            else if (dateDiff > numberOfDays) {
                alertify.alert("Date range cannot be greater than " + numberOfDays + " days.");
                return false;
            }

            else {
                return true;
            }
        });
    }
}

Calling the function

import * as common from "../../Common/Common.js"
let dateHelpers = new common.CheckMaxDateRange();
dateHelpers.CheckMaxDateRange($('#StartDate').val(), $('#EndDate').val(), 365)

Where am I going wrong here? All of the exported functions I have used before haven't needed any dependencies.

Thanks in advance!

I don't think your export is the problem since you can export your class as:

export class X {...}
export class Y {...}

or

class X {...}
class Y {...}
export { X, Y }

I think the issue could be on how you are importing your exported class:

import { DateHelpers} from "./path_to_class/DateHelpers";
let dateHelpers = new DateHelpers();

check if your dateHelpers variable is undefined or not prior to call the method. It will help you to figure out where the issue could be.

Keep me posted if this helped or not so I can provide further assistance.

Don't use require with ES6 module syntax. You should import the dependencies of your module, and configure your compiler/bundler to support loading of external scripts (possibly by emitting AMD syntax modules if you want to use the require.js library).

Also you shouldn't use class es that have no state and just a single method. Write

import * as moment from "moment";
import * as alertify from "alertify";

export function checkMaxDateRange(fromDate: string, toDate: string, numberOfDays: Number) {
    moment.locale('en');
    var momToDate = moment(toDate, "MM/DD/YYYY");
    var momFromDate = moment(fromDate, "MM/DD/YYYY");

    var dateDiff = momToDate.diff(momFromDate, 'days');

    if (dateDiff < 0) {
        alertify.alert("From Date must be before To Date.");
        return false;
    }

    else if (dateDiff > numberOfDays) {
        alertify.alert("Date range cannot be greater than " + numberOfDays + " days.");
        return false;
    }

    else {
        return true;
    }
}

import * as common from "../../Common/Common.js";

common.checkMaxDateRange($('#StartDate').val(), $('#EndDate').val(), 365)

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