导入和导出时的基本Typescript应用程序问题

Basic Typescript app issue with importing and exporting

本文关键字:Typescript 应用程序 问题 导入      更新时间:2023-09-26

我有一个非常基本的Typescript应用程序,Chrome在打开我的index.html页面时抱怨如下:

出口未定义

导入未定义

app.ts

import Timer from "./models/timer";
class Startup {
    public static main() {
        // Initialize timer
        var timer = new Timer(new Date("25/12/2015 00:00:00"));
        // Start timer
        timer.start();
    }
}
Startup.main();

型号/定时器.ts

export default class Timer {
    target: Date;
    days: number;
    hours: number;
    minutes: number;
    seconds: number;
    constructor(target: Date) {
        this.target = target;
    }
    start() {
        var now = Date.now;
        alert(now);
    }
}

我使用的是Visual Studio代码,并有以下内容:

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true
    }
}

tasks.json

{
    "version": "0.1.0",
    "command": "tsc",
    "isShellCommand": true,
    "args": ["-p", "."],
    "showOutput": "silent",
    "problemMatcher": "$tsc"
}

来自您的错误:

exports is not defined
import is not defined

你需要一个模块加载程序!浏览器本身还不能理解模块。建议使用webpack(其他选项包括浏览等)。

更多

快速启动:https://basarat.gitbook.io/typescript/content/docs/quick/browser.html