首页 文章

在Angular2 app中使用typings定义

提问于
浏览
0

我是打字稿并使用Angular 2应用程序的新手 . 我需要访问一些js库,其类型定义可从absolutetyped获得 . 我使用typings安装了依赖项 . 但无法弄清楚如何在我的应用程序组件中访问它们 .

更具体地说,我安装angular-toastr库:

npm install angular-toastr  --save
typings install angular-toastr  --ambient --save

这会将angular-toastr依赖项安装到 /typings/main/ambient/angular-toastr/angular-toastr.d.ts

然后我将对此的引用添加到 /typings 文件夹中的main.d.ts文件中 .

/// <reference path="main/ambient/angular-toastr/angular-toastr.d.ts" />

在此之后,我尝试直接在我的组件中引用它,但还没有弄清楚如何 .

我错过了其他任何步骤吗?我如何将其导入组件并使用提供的方法?

谢谢

1 回答

  • 2

    我正在尝试相同的,我能够添加两个类型定义文件并成功使用它 . 那么让我分享一下我的所作所为 .

    第1步:安装 npm install typings --global 第2步:打字安装dt~lodash --global --save或

    https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/index.d.ts使用直接路径

    安装 .

    安装完成后,我们需要将基本参考文件引用到tsconfig.json文件中 .

    enter image description here

    enter image description here

    "files": [
            "typings/index.d.ts"
        ]
    

    现在我们可以开始使用第三方库了,

    import * as $ from 'jquery';
    import * as _ from "lodash";
    

    添加到我们需要使用librabry功能的任何ts文件 .

    enter image description here

    结果是,

    enter image description here

相关问题