错误TS2304:找不到名称“嚎叫”

问题描述 投票:3回答:2

我在我的应用程序中使用吼包。我安装使用npm install --save @types/howler的分型,在我的index.html我已经包含下面的脚本标签

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

但它仍然显示错误error TS2304: Cannot find name 'Howl'当我尝试使用NG服务命令为我的应用程序。

angular
2个回答
5
投票

安装号哭和打字稿定义:

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

你并不需要把它导入到你的index.html文件,只需要导入howler直接在您的组件类:

import { Howl } from 'howler';

3
投票

有时候,如果你有没有modules导入库,你只需要与申报关键字declare the variable

例如,离开你的脚本在您的index.html:

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

但随后在组件(如果你确信这个脚本将被载入),你可以声明嚎叫:

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 $;


Found good source for others facing this same issue:
https://basarat.gitbooks.io/typescript/docs/why-typescript.html
https://github.com/DefinitelyTyped/DefinitelyTyped
© www.soinside.com 2019 - 2024. All rights reserved.