问题
I have the following code to load MonacoEditor in index.html
of my AngularJS website:
<link rel="stylesheet" data-name="vs/editor/editor.main" href="/monaco-editor/min/vs/editor/editor.main.css" />
<script src="/monaco-editor/min/vs/loader.js"></script>
<script src="/monaco-editor/min/vs/editor/editor.main.nls.js"></script>
<script src="/monaco-editor/min/vs/editor/editor.main.js"></script>
<script>
require.config({ paths: { 'vs': '/monaco-editor/min/vs' }})
console.log(monaco)
</script>
Running the website displays well monaco
, which will be used in a further JavaScript file.
Now, I want to load MonacoEditor by ocLazyLoad
:
.state('addin', {
abstract: true,
template: '<ui-view/>',
resolve: {
loadAddinCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load({files: [
"/monaco-editor/min/vs/editor/editor.main.css",
"/monaco-editor/min/vs/loader.js",
"/monaco-editor/min/vs/editor/editor.main.nls.js",
"/monaco-editor/min/vs/editor/editor.main.js"
]}).then(function () {
require.config({ paths: { 'vs': '/monaco-editor/min/vs' }})
console.log(monaco)
})
}]
}
})
The above code returns ReferenceError: monaco is not defined
. Does anyone know why this happens?
Actually, I don't understand well the purpose of require.config
, it seems to make the code much less flexible. Does anyone have an alternative to that?
回答1:
You have loaded the dependencies, yet not loaded Monaco. Try this after your require.config
:
require.config({ paths: { 'vs': '/monaco-editor/min/vs' }})
require(['vs/editor/editor.main'], function onMonacoLoaded(){
console.log(monaco);
});
来源:https://stackoverflow.com/questions/50017351/lazy-load-monacoeditor