Typescripts - Uncaught ReferenceError: toastr is not defined

Typescripts - Uncaught ReferenceError: toastr is not defined

谁能帮帮我。我使用 Microsoft Visual Studio 2015 在 TypeScripts 中做了一个简单的项目,我从 git 存储库中添加了 2 个库 "toastr" 和 "jquery":https://github.com/DefinitelyTyped/DefinitelyTyped. Once added, Visual Studio shows no error or warning at all (like in the picture Visual Studio Screen )。但是当我 运行 应用程序时,这就是我收到的:未捕获的 ReferenceError:未定义 toastr。

... 和 index.html :

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
    <script src="app.js"></script>
</head>
<body>
    <h1>TypeScript HTML App</h1>

    <div id="content"></div>
</body>
</html>

app.ts:

/// <reference path="toastr.d.ts" />


class Greeter {
    element: HTMLElement;
    span: HTMLElement;
    timerToken: number;

    constructor(element: HTMLElement) {
        this.element = element;
        this.element.innerHTML += "The time is: ";
        this.span = document.createElement('span');
        this.element.appendChild(this.span);
        this.span.innerText = new Date().toUTCString();
    }

    start() {
        this.timerToken = setInterval(() => this.span.innerHTML = new Date().toUTCString(), 500);
    }

    stop() {
        clearTimeout(this.timerToken);
    }

}

window.onload = () => {
    var el = document.getElementById('content');
    var greeter = new Greeter(el);
    greeter.start();
    toastr.info("ASDFGH");
};

您已经添加了对 toastr 类型定义的引用,但是您实际上并没有引用 toastr 或 jquery js 库。 因此,将 toastr.js 和 css 文件添加到您的 html(更多信息请点击此处 https://github.com/CodeSeven/toastr)。 例如在 head 元素中:

<link href="build/toastr.min.css" rel="stylesheet" type="text/css">

然后是内容

<script src="build/toastr.min.js"></script>

看看示例中的 html,让您更好地了解它应该是什么样子:http://codeseven.github.io/toastr/demo.html

所以,澄清一下:*.d.ts 文件只是告诉 TypeScript 编译器您正在引用的外部文件中的类型(用于设计时编译)。他们实际上并没有在 运行 时间内引入任何 js。