Angular2 issues to load external javascripts

2019-09-05 18:09发布

问题:

I am new in agnular2. I have used html5 theme and theme having multiple javascript files. Now the issue is I am trying to load that files in my angular2 app but it's not loading properly.

What I have did is just link up that scripts to my index.html file and first time when I open the page the scripts are working good but when I route the pages in my app then it's not loading into the app on each router change need to refresh the page.

Please help..!!!

回答1:

it's because your JS file are loaded before the components are generated. Try something like this: In your js file use constructor fn. /**/ app.js

function callSomething(){
  console.log('something has been called.');
}

/* in component.ts*/

declare var callSomething: any; //declare this at top;

now you can call it inside any method: new callSomething();



回答2:

I think you need to initialise it on your view.I have done something like this:

In app.js(which i have included in my index.html)

function scrollToId(id){
  //scroll fn
}

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

declare var scrollToId: any;
@Component({})
export class UserDetailsComponent implements OnInit {
  constructor(){}
  ngOnInit() {
    new scrollToId('registration-form');
  }
}

& it's working fine for me.



标签: angular