Javascript 从Typescript中的模块导入的函数为';在编译的JS中,t是否被识别为函数?
我有一个Typescript项目,它从Javascript 从Typescript中的模块导入的函数为';在编译的JS中,t是否被识别为函数?,javascript,typescript,Javascript,Typescript,我有一个Typescript项目,它从module.js模块调用函数。这是我的初始代码: //app.ts import { MobileServiceUser, setCache, getCache } from "nativescript-azure-mobile-apps/user"; export function onLoginTap(args) { console.log("tap"); ai.busy = true; var cache = getCache
module.js
模块调用函数。这是我的初始代码:
//app.ts
import { MobileServiceUser, setCache, getCache } from "nativescript-azure-mobile-apps/user";
export function onLoginTap(args) {
console.log("tap");
ai.busy = true;
var cache = getCache();
}
如果我使用VSCode“转到定义”功能,则getCache()
转到顶部的import语句,如果我再次执行此操作,则转到:
//module.ts
declare module "nativescript-azure-mobile-apps/user" {
export class MobileServiceUser {
mAuthenticationToken: string;
mUserId: string;
constructor(o: { userId: string; });
}
export function setCache(user: MobileServiceUser): void;
export function getCache(): MobileServiceUser;
}
当我执行代码时,var cache=user_1.getCache()代码>行被执行,我的应用程序崩溃,抛出错误:
TypeError:user_1.getCache不是函数
文件:“/data/data/inc.tangra.azuremobileservicessample/files/app/main-page.js,第94行,第23列
app.ts看起来像是编译成js的:
var user_1 = require("nativescript-azure-mobile-apps/user");
function onLoginTap(args) {
console.log("tap");
ai.busy = true;
var cache = user_1.getCache();
}
为什么代码不能识别导入的函数
更新:这显示了项目结构(代码的第一页在sample/main-page.ts中,您实际的“nativescript azuer mobile apps/user.js或.ts文件”在哪里?@Nathanel,它在node_modules文件夹中。我正在成功地使用从该模块导出的其他类在您的模块.ts
中,我不确定typescript(从未使用过它),但我知道,如果在对象中放一个分号,JS就会断然拒绝,就像在构造函数中一样(o:{userId:string;})
尝试删除字符串后的分号,看看是否有帮助,或者错误是否发生了变化。@Jhecht我恐怕还是会得到相同的结果…为什么在文件.ts
中声明模块?