I am trying to use MathJax to display equations in my Angular 2
app. Only a small bit of my app actually needs this functionality, so I don't want all users to have to download the 3-400kb needed.
My initial thought was to import "mathjax"
within my component, so that the library will be loaded only when the component is created. However, I learned from my earlier question that MathJax doesn't play well with others because it uses its own custom module loader.
My current setup loads the script
from my index.html
but that means everyone will download it when only a small percentage of users need this component
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_SVG"></script>
Is there a clean way in Angular 2
to have my component download a script within the ngOnInit()
hook and execute a callback once it's in the DOM? Something such as (pseudo-code):
ngOnInit(){
//download script, add to DOM
fetch('https://...MathJax.js').then(addToDom).then(this.onMathJaxLoaded);
}
onMathJaxLoaded(){
// Run math renderer
MathJax.Hub.Queue("Typeset",...);
}