简体   繁体   English

将RequireJS与导出的类一起使用时函数结果未定义

[英]Function result undefined when using RequireJS with exported class

I'm using Typescript, Require, and AMD modules. 我正在使用Typescript,Require和AMD模块。 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. 如果我不使用require([在导出的类中,它将无法编译。

Exported class from common.ts file. 从common.ts文件导出的类。

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. 在调用该方法之前,请检查您的dateHelpers变量是否未定义。 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. 不要在ES6模块语法中使用require 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). 您应该import模块的依赖项,并配置编译器/捆绑程序以支持外部脚本的加载(如果要使用require.js库,可以通过发出AMD语法模块)。

Also you shouldn't use class es that have no state and just a single method. 此外,你不应该使用class是没有状态,只是一个单一的方法上课。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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