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


  出口没有定义
  
  导入未定义


应用程序

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();


型号/计时器

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
    }
}


task.json

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

最佳答案

从您的错误:

exports is not defined

import is not defined


您需要一个模块加载器!浏览器本身还不了解模块。推荐使用webpack(其他选项包括browserify等)。

更多

快速入门:https://basarat.gitbook.io/typescript/content/docs/quick/browser.html

10-07 12:55
查看更多