How Import JS File in angular 6?

前端 未结 4 1434
时光取名叫无心
时光取名叫无心 2021-02-10 09:19

how can I import a js file in angular 6. I tried to implement a navbar and it contains js functions. thanks!!!

I Add my js file called nav.js

 \"scripts         


        
4条回答
  •  广开言路
    2021-02-10 09:45

    I suggest some workaround if you can grasp the functions within the script. So You either put them in your component.ts code as functions ,or if you can't/don't want to , you will just have to export/import them . for example

    myscript.js

      function a(){ ...}
        function b(){...}
    
    
        module.exports.a=a
        module.exports.b=b
    

    mycomponent.component.ts

    ..
    import a from './path/../myscript.js';
    import b from './path/../myscript.js';
    
    ..
    constructor(){
    ...
    a.a()
    b.b()
    ...
    }
    

    ps:many times you will find function anonymous so you just have to rename them , for example

    myscript.js

    function(){..}();

    will be

    myscript.js

    function myCustomName(){..};
    module.exports.customName=myCustomName
    

提交回复
热议问题