Lazy-load MonacoEditor

风流意气都作罢 提交于 2019-12-08 08:10:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!