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)

1
  • 1
    Anybody looking at this in the future and having issues that involve routed components might want to have a look at this question. There are some subtleties when dealing with routing. Commented Mar 22, 2016 at 19:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am getting ReferenceError: componentHandler is not defined .. I'm not sure why.. any ideas ?
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" />  

Comments

0

I guess you need to add

declare componentHandler;
componentHandler.upgradeDom();

See also Calling JavaScript directly from TypeScript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.