assuming I have a js file mylib.js
in an angular 7 app, located in assets/mylib.js
:
mylib = function(){
return {
hi: function() { alert('hi'); }
};
}();
and I want in my hero-form.component.ts
to be able to call mylib.hi()
, what would be the proper way to do this ?
I tried to the same as the jquery package that I installed and it works so I added in angular.json
-> projects/myproject/architect/build/options/scripts
like this:
"scripts": ["node_modules/jquery/dist/jquery.js", "src/assets/mylib.js"]
and in hero-form.component.ts
:
import * as $ from "jquery"; // works, installed via npm
import * as mylib from "mylib"; // cannot find module mylib
I test it by calling in the component:
ngOnInit() {
$('body').css('background','gainsboro');
mylib.hi();
}
please note, I don't want to add the scripts in the index.html header globally, I want to call import for each component that needs it