-
-
Save VaLeXaR/6b79b1ff388e00078cb53f8f1f9ef082 to your computer and use it in GitHub Desktop.
Lazy Load External JavaScript Library in Angular #tags: Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare var Quill: any; | |
@Component({ | |
//... | |
}) | |
export class QuillEditorComponent implements OnInit { | |
constructor( | |
// ... | |
private readonly svc: QuillEditorService, | |
@Inject(DOCUMENT) private readonly document: any | |
) {} | |
ngOnInit() { | |
this.svc.lazyLoadQuill().subscribe(_ => { | |
if (!Quill) { | |
Quill = this.document.defaultView.Quill; | |
} | |
this.setupQuill(); | |
}); | |
} | |
setupQuill() { | |
if (!Quill) { | |
return; | |
} | |
// set up External JS library | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Inject } from '@angular/core'; | |
import { ReplaySubject, Observable, forkJoin } from 'rxjs'; | |
import { DOCUMENT } from '@angular/common'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class QuillEditorService { | |
private _loadedLibraries: { [url: string]: ReplaySubject<any> } = {}; | |
constructor(@Inject(DOCUMENT) private readonly document: any) {} | |
lazyLoadQuill(): Observable<any> { | |
return forkJoin([ | |
this.loadScript('assets/quill/quill.min.js'), | |
this.loadStyle('assets/quill/quill.snow.css') | |
]); | |
} | |
private loadScript(url: string): Observable<any> { | |
if (this._loadedLibraries[url]) { | |
return this._loadedLibraries[url].asObservable(); | |
} | |
this._loadedLibraries[url] = new ReplaySubject(); | |
const script = this.document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.async = true; | |
script.src = url; | |
script.onload = () => { | |
this._loadedLibraries[url].next(); | |
this._loadedLibraries[url].complete(); | |
}; | |
this.document.body.appendChild(script); | |
return this._loadedLibraries[url].asObservable(); | |
} | |
private loadStyle(url: string): Observable<any> { | |
if (this._loadedLibraries[url]) { | |
return this._loadedLibraries[url].asObservable(); | |
} | |
this._loadedLibraries[url] = new ReplaySubject(); | |
const style = this.document.createElement('link'); | |
style.type = 'text/css'; | |
style.href = url; | |
style.rel = 'stylesheet'; | |
style.onload = () => { | |
this._loadedLibraries[url].next(); | |
this._loadedLibraries[url].complete(); | |
}; | |
const head = document.getElementsByTagName('head')[0]; | |
head.appendChild(style); | |
return this._loadedLibraries[url].asObservable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment