gpt4 book ai didi

node.js - TypeScript导出/导入功能

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:58 26 4
gpt4 key购买 nike

我编写了一个模块(通过 npm 从私有(private)存储库安装),其结构如下:

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

在我的 email.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);

但是,这总是会引发错误,指出“无法读取未定义的属性“sendEmail””

为什么会发生这种情况?以这种方式导出时我不应该能够使用这种类型的声明吗?有什么办法解决这个问题吗?

致以诚挚的问候

最佳答案

尝试改变

export default function sendEmail

只是

export function sendEmail

关于node.js - TypeScript导出/导入功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com