How to Include JS file in ionic 3

前端 未结 5 1771
慢半拍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:39

    None of above solutions worked for me, In first solution js file loads at the time of application loading that's not perfect solution when you have bulk js file.

    I was looking for dynamic solution to load the external library and there is library for loading asynchronous JavaScript files. https://www.npmjs.com/package/scriptjs

    Install the package:

    npm i scriptjs
    

    Then use it anywhere like below:

    import { get } from 'scriptjs';
    
    ngOnInit() {
    
        get("assets/js/searchEmp.js", () => {
            getSerchInspContext = this;
            loadSearchEmp();
        });}
    

    OR

    You can simply use the jquery method to append or remove the script tag in your header.

    To add .js file, call below line under ngOnInit():

    $('head').append('');
    

    Remove .js file:

    document.querySelector('script[src="assets/js/search.js"]').remove();
    

提交回复
热议问题