Make Angular 2 component download and run a script
我正在尝试使用Mathjax在我的
我最初的想法是在我的组件中使用
我当前的设置从我的
1 | <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_SVG"> |
在
1 2 3 4 5 6 7 8 | ngOnInit(){ //download script, add to DOM fetch('https://...MathJax.js').then(addToDom).then(this.onMathJaxLoaded); } onMathJaxLoaded(){ // Run math renderer MathJax.Hub.Queue("Typeset",...); } |
我没有尝试过,但我认为这是可能的:
1 2 3 4 5 6 7 8 9 10 11 | ngOnInit(){ //download script, add to DOM var script = document.createElement('script'); document.body.appendChild(script) script.onload = this.onMathJaxLoaded.bind(this); script.src = 'https://...MathJax.js'; } onMathJaxLoaded(){ // Run math renderer MathJax.Hub.Queue("Typeset",...); } |
下面是一个fiddle,它包含一个简单的javascript示例;https://jsfiddle.net/5qu5h7bc/
基于@TryingToimprove的解决方案,我决定在我的应用程序中添加一个
以下是如何使用它:
1 2 3 | this.scriptLoaderService.load('https://...js').subscribe( ()=>console.log('script has loaded'); ); |
服务内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /** * A directory of scripts that have been or are being loaded */ private scripts = new Map<string,Observable<boolean>|boolean>(); /** * Downloads script; returns Observable that emits TRUE once"load" event fires */ public load(src:string):Observable<boolean>{ if(this.scripts.has(src)){ // If script has already been fully loaded. if(this.scripts.get(src)===true) return Observable.of(true); // Else if download is already underway but load event hasn't fired yet else return this.scripts.get(src); } //Add a script tag to the DOM let script = document.createElement('script'); document.body.appendChild(script); // upon subscription, listen for the load event. Once it arrives, emit TRUE let obs:Observable<boolean> = Observable.fromEvent(script,'load') // Map should hold TRUE once script is loaded .do(()=> this.scripts.set(src,true)) .take(1).map(e=>true); // set the src attribute (will cause browser to download) script.src = src; //add observable to the scripts Map this.scripts.set(src,obs); return obs; } |
在