【问题标题】:Error TS2304: Cannot find name 'Howl'错误 TS2304:找不到名称“嚎叫”
【发布时间】:2019-02-05 19:04:09
【问题描述】:

我在我的应用程序中使用了 howler 包。我使用 npm install --save @types/howler 并在我的 index.html 中包含以下脚本标记

<script src="./node_modules/howler/dist/howler.min.js"></script>

但当我尝试使用 ng serve 命令为我的应用程序提供服务时,它仍然显示错误 error TS2304: Cannot find name 'Howl'

【问题讨论】:

  • 您可能需要将它添加到您的 app.module.ts 中的导入中。只是一个猜测,但大多数东西都需要放在那里

标签: angular


【解决方案1】:

安装 howler 和 typescript 定义:

npm install howler --save
npm install @types/howler --save

你不需要将它导入到你的 index.html 文件中,直接在你的组件类中导入howler

import { Howl } from 'howler';

【讨论】:

    【解决方案2】:

    有时,如果您有一个库没有要导入的modules,您只需要使用关键字声明declare the variable

    例如,将脚本留在 index.html 中:

    <script src="./node_modules/howler/dist/howler.min.js"></script>
    

    但是在你的组件中(如果你确定这个脚本会被加载),你可以声明 Howl:

    import { Component } from '@angular/core';
    
    declare var Howl; <-- declare above @Component
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular 5';
    }
    

    注意:你通常会在 Jquery 中看到这个:declare var $;


    为面临同样问题的其他人找到了很好的来源:
    https://basarat.gitbooks.io/typescript/docs/why-typescript.html
    https://github.com/DefinitelyTyped/DefinitelyTyped

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2016-12-18
      • 2019-05-06
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多