【问题标题】:Cant get a reference to ACE using requirejs无法使用 requirejs 获得对 ACE 的引用
【发布时间】:2013-10-02 15:56:39
【问题描述】:

http://plnkr.co/edit/bSm4nYJ0TmUqylidxkqt?p=preview

我正在尝试使用 requirejs 来注入对 ACE 编辑器 JavaScript 库的引用。 嵌套 ace 引用的对象返回得很好,但该对象的 ace 属性未定义。我的设置中缺少什么?

代码如下。

我使用定义返回一个对象。 define 函数声明了对本地 ace 库的依赖(控制台显示加载的 ace.js 资源)。

define(["./libs/ace/ace"],function(ace){
return {
        ref: ace,
        msg: "alive"
    }

});

我的 requirejs 数据主要目标是我的 script.js 文件,如下所示...

require(["js/testace"],function(testace){console.log(testace);});

我的文件结构如下...

...而我的控制台输出如下

对象 {ref: undefined, msg: "alive"}

【问题讨论】:

    标签: javascript requirejs


    【解决方案1】:

    我通过使用以下代码得到它: 将/static/ace 替换为lib/ace。

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>ACE in Action</title>
    <style type="text/css" media="screen">
        #editor { 
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
        }
    </style>
    </head>
    <body>
    
    <div id="editor">
        function foo(items) {
        var x = "All this is syntax highlighted";
        return x;
        }
    </div>
    <script type="text/javascript" src="/static/require.js"></script>
    <script>
        require.config({
            baseUrl: window.location.protocol + "//" + window.location.host
                + window.location.pathname.split("/").slice(0, -1).join("/"),
    
            paths: {
                ace: "/static/ace"
            }
        });
    
        require(["ace/ace"], function (ace) {
            var editor = ace.edit("editor");
            editor.setTheme("ace/theme/monokai");
            editor.getSession().setMode("ace/mode/javascript");
        });
    </script>
    </body>
    </html>
    

    来源:https://github.com/ajaxorg/ace/issues/1017

    如果您遇到一些疯狂的错误,请查看此页面:http://requirejs.org/docs/errors.html

    最初在这里回答:How to load ace editor

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 2010-10-03
      • 1970-01-01
      相关资源
      最近更新 更多