【问题标题】:How to use DefinitelyTyped in Angular5?如何在Angular5中使用DefiniteTyped?
【发布时间】:2018-03-04 18:34:40
【问题描述】:

如果我是正确的,DefinitelyTyped 有多个库的 TypeScript 定义。就我而言,我想使用可用的谷歌图表定义here。 我已经安装了它们使用

npm install --save-dev @types/node
npm install --save-dev @types/google-apps-script

但我不知道如何将它们导入我的项目。我试过使用

import { Google } from '@types/google-apps-script';

但是 Visual Studio Code 抛出一个错误,它不是一个模块,我的 declare const google 仍然是 :any,我想避免这种情况。

任何帮助或提示将不胜感激。

【问题讨论】:

    标签: google-visualization angular5 definitelytyped


    【解决方案1】:

    为了让 Google Charts 在我的 Angular 5 项目中工作,我使用了这些肯定类型的类型,而这些类型仅适用于 Google Charts JavaScript 库。

    @types/google.visualization
    

    https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google.visualization

    这是我将类型导入@Injectable Service 类的方法。这条语句并没有引发关于它不是模块的错误,就像我尝试使用 * 导入时那样。

    import { } from 'google.visualization';
    

    在构造函数中,您可以只加载图表库一次,然后创建图表函数以将数据和配置选项传递给回调 (setOnLoadCallback)。

    google.charts.load('current', {'packages':['corechart']}); 
    

    我在 index.html 中添加了一个脚本元素来加载“https://www.gstatic.com/charts/loader.js”,但也许有更好的方法来包含诸如此类的外部库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-05
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      相关资源
      最近更新 更多