4

I have added the following bit of code in my angular2 app to help MDL re-register the components when moving around the app...

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)

dazziep
  • 245
  • 1
  • 4
  • 16
  • 1
    Anybody looking at this in the future and having issues that involve routed components might want to have a look at [this question](http://stackoverflow.com/questions/36162649/angular2-router-interacting-with-material-design-lite/36163652#36163652). There are some subtleties when dealing with routing. – Michael Tiller Mar 22 '16 at 19:42

3 Answers3

3

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.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
2

if you are using cli.angular tool for generating your app do this, so that no need to duplicate the code everywhere.

  1. add below line into typings.d.ts file

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

       /// <reference path="../typings.d.ts" />  
Srini
  • 708
  • 1
  • 8
  • 23
0

I guess you need to add

declare componentHandler;
componentHandler.upgradeDom();

See also Calling JavaScript directly from TypeScript

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567