打字稿导出/导入功能 [英] TypeScript export/import function

查看:0
本文介绍了打字稿导出/导入功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我编写的模块(通过NPM从私有repo安装),它的结构如下:

email.ts、utils.ts、index.ts(一些其他文件)

在我的电子邮件中,我有以下功能:

export default function sendEmail(toEmail: string, subject: string, content: string): any {

    let helper = SendGrid.mail;

    let from_email: any = new helper.Email(process.env.FROM_EMAIL);
    let to_email: any = new helper.Email(toEmail);
    let helperContent: any = new helper.Content("text/plain", content);
    let mail: any = new helper.Mail(from_email, subject, to_email, helperContent)

    var request = SendGrid.emptyRequest({
        method: "POST",
        path: "/v3/mail/send",
        body: mail.toJSON()
    });

    //This performs the request with a promise
    SendGrid.API(request).then((response: any) => {
        //Deal with output as needed
        console.log("email was sent!!");
    }).catch((err: any) => {
        //log.error(err);
    });
}

然后,在我的index.ts上,我有以下声明:

export * from "./email";
export * from "./errors";
export * from "./logger";
export * from "./objectUtils";
export * from "./queryFilter";
export * from "./templateEngine";
export * from "./utils";

在我导入此模块的应用程序上,我将其导入为

import * as Utils from "my-utils";

最后,每当我想要使用sendEmail函数时,我都会使用以下语句来调用它:

Utils.sendEmail(email, subject, content);

但是,这始终会引发错误,指出"Cannot read property 'sendEmail' of undefined"

为什么会发生这种情况?我不应该在以这种方式导出时使用这种类型的声明吗? 此问题的解决方案是什么?

致以最良好的问候

推荐答案

尝试更改

export default function sendEmail

export function sendEmail

这篇关于打字稿导出/导入功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆