简体   繁体   English

Angular2与Material Design Lite

[英]Angular2 with Material Design Lite

I have added the following bit of code in my angular2 app to help MDL re-register the components when moving around the app... 我在angular2应用程序中添加了以下代码,以帮助MDL在应用程序中移动时重新注册组件...

ngAfterViewInit() {
    componentHandler.upgradeDom();
}

And although it seems to be working ok (as expected) I am getting the following error... 虽然它似乎工作正常(如预期)我收到以下错误...

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

I'm still quite new to angular2 and typescript but I guess I need to import something so my code knows what componentHandler is(even though it must know what it is because it works and doesn't work without this code??? confused) 我仍然是angular2和typescript的新手,但我想我需要导入一些东西,所以我的代码知道什么是componentHandler(即使它必须知道它是什么,因为它工作,如果没有这段代码就无法工作?

It should help you to add 它应该可以帮助您添加

declare var componentHandler: any;

at the top of your code. 在代码的顶部。 Please refer to the corresponding handbook section on Working with Other JavaScript Libraries in TypeScript. 请参阅TypeScript中有关使用其他JavaScript库的相应手册部分

if you are using cli.angular tool for generating your app do this, so that no need to duplicate the code everywhere. 如果您使用cli.angular工具生成应用程序,请执行此操作,以便无需在任何地方复制代码。

  1. add below line into typings.d.ts file 将以下行添加到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();

See also Calling JavaScript directly from TypeScript 另请参阅直接从TypeScript调用JavaScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM