3

I have a legacy script that I need to include in my angular application. The thing about this script is that it relates to a specific component, and it has to be loaded only after the view of the component is loaded. As for today, I succeeded to include it on OnInit function but sometimes (not always for some reason) the CLI throws an error about it.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-player-page',
  templateUrl: './player-page.component.html',
  styleUrls: ['./player-page.component.scss']
})
export class PlayerPageComponent implements OnInit {
  public itemId: string;
  
  constructor() {}

  ngOnInit() {
    //We loading the player srcript on after view is loaded
    require('assets/scripts/player/player.js');
  }

}

The script assumes that elements in the UI exists and it searches them by the id. When adding it on top of the page, it doesn't work. What is the best way to achieve the desired behavior?

5
  • I had a similar problem with Google Maps. The solution I found was to create the <script> tag and inserting it in the head / body. Commented Jul 3, 2018 at 8:12
  • What error does the cli throws? and on ngOnInit, the view is not rendered yet, you should use ngAfterViewInit. Probably wouldn't matter, because loading the js should take longer than the app to go from onInit to afterViewInit Commented Jul 3, 2018 at 8:27
  • ERROR in src/app/player-page/player-page.component.ts(14,5): error TS2304: Cannot find name 'require'. @PierreDuc thanks for the help. I changed it to ngAfterViewInit Commented Jul 3, 2018 at 8:49
  • @Jorgeblom - Angular ignore script tags in components HTML as far as I know Commented Jul 3, 2018 at 8:51
  • @TalHumy just add declare const require: any; on top of your component, you can also use the standard typescript dynamic import: import('assets/scripts/player/player.js'); Commented Jul 3, 2018 at 8:57

1 Answer 1

5

There are multiple solutions to this issue.

  1. declare the require const on top of your component

    declare const require: any;
    
    import { Component, OnInit } from '@angular/core';
    
    @Component({})
    ...
    
  2. use the dynamic import() function from typescript

    ngAfterViewInit() {
      //We loading the player script on after view is loaded
      import('assets/scripts/player/player.js');
    }
    
  3. change the library to only start running after you call a function from the component, this way you can add it to the scripts array of your angular.json

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

1 Comment

Using import solved it - so simple I don't know why I used require. The third option is great but it will force me to change other projects. Thank you very much

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.