RequireJS load string

后端 未结 3 1410
隐瞒了意图╮
隐瞒了意图╮ 2021-02-10 01:47

In my app there are dynamic parts that are loaded from database as string that looks like:

\"define([\'dependency1\', \'dependency2\'], function(){\"+
\"   // fu         


        
3条回答
  •  -上瘾入骨i
    2021-02-10 02:17

    You should be able to do :

    require(["jquery"], function($){        
        $(document).ready(function(){
            // logic to load specific script from database
            var scriptString = functionToLoadTheStringAbove();
    
            var olddefine = define; // require js define 
            var runme; // capture function 
            define = function (args,func){
                 runme = func; 
            }
            eval(scriptString);
            runme(); // run the function in current scope
            define = olddefine; // restore requirejs function 
    
            // dependency1, dependency2 are loaded        
        });
    });
    

提交回复
热议问题