Angular2 with Material Design Lite

Angular2 with Material Design Lite

本文关键字:Lite Design Material with Angular2      更新时间:2023-09-26

我在我的 angular2 应用程序中添加了以下代码,以帮助 MDL 在应用程序中移动时重新注册组件......

ngAfterViewInit() {
    componentHandler.upgradeDom();
}

虽然它似乎工作正常(如预期的那样(,但我收到以下错误......

(16,5): error TS2304: Cannot find name 'componentHandler'.

我对 angular2 和打字稿仍然很陌生,但我想我需要导入一些东西,以便我的代码知道 componentHandler 是什么(即使它必须知道它是什么,因为它可以工作并且没有这段代码就不起作用???困惑(

应该可以帮助您添加

declare var componentHandler: any;

在代码的顶部。请参考相应的手册部分,在 TypeScript 中使用其他 JavaScript 库

如果您正在使用 cli.angular 工具来生成应用程序这样做,这样就不需要到处复制代码。

  1. 将以下行添加到 typings.d.ts 文件中

       declare var componentHandler: any;  
  2. reference the file into your component file as below

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

I guess you need to add

declare componentHandler;
componentHandler.upgradeDom();

另请参阅直接从 TypeScript 调用 JavaScript