[英]How to import Javascript module in Typescript
我想知道在Typescript中导入Javascript模块。
项目
///<reference path=''/>
控制内部模块 码
app.js
export function func(a, b) {
return a+b;
}
MainApp.ts
import Stats from "../app";
class MainApp {
foo() {
const f = func(1, 2); // not define (runtime error)
}
}
错误
SyntaxError: Unexpected token export
ReferenceError: define is not defined
Main.js:6667
at d:\...\Main.js:6667:2
ReferenceError: MainApp is not defined
at window.onload (d:\...\index.html:18:24)
找不到定义。
默认导出可能有误。 这是适合您的代码结构的工作示例。
tsconfig.json:
{
"compilerOptions": {
"strict": true,
"allowJs": true,
"target": "es6",
"module": "commonjs"
}
app.js:
export function sum(a, b) {
return a + b;
}
MainApp.ts:
import {sum} from './app';
class MainApp {
foo() {
const a = 1;
const b = 2;
const result = sum(1, 2);
}
}
global.d.ts:
declare module '*.js';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.