How to Include JS file in ionic 3

前端 未结 5 1765
慢半拍i
慢半拍i 2020-12-13 10:10

Im looking for a way to access a variable from external js file which i included in assets/data folder

below is what i tried

placed test.js

5条回答
  •  时光取名叫无心
    2020-12-13 10:50

    This solution only worked for me

    Put the import js in src/index.html header tag, before the build/polyfills.js and build/main.js (they are in body tag);

    Example : I created a file src/assets/test.js with a var testvar, imported in src/index.html and then in src/app/app.component.ts declared declare var testvar;.

    test.js

    var testvar = "Hello from external js";
    

    index.html

    ...
      
      
      
    
      
      
       //here, not in body
    ...
    

    app.componet.ts

    declare var testvar;
    
    @Component({
       templateUrl: 'app.html'
    })
    export class MyApp {
      @ViewChild(Nav) nav: Nav;
      constructor(private statusbar : StatusBar,  splashScreen: SplashScreen) {
       alert(testvar);
    ...
    

提交回复
热议问题