我有一个我写的模块(通过npm从私人仓库安装),结构如下:
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);
然而,这总是会引发错误,指出"Cannot read property 'sendEmail' of undefined"
。
为什么会这样?导出这种方式时,我不能使用这种类型的声明吗? 解决方案是什么?
最好的问候
答案 0 :(得分:5)
尝试更改
export default function sendEmail
到
export function sendEmail