javascript 王牌编辑器“定义未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10883190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:20:34  来源:igfitidea点击:

Ace editor "define is not defined"

javascriptace-editor

提问by Stinky

I'm trying to add the ace editorto my app. I downloaded it from github, dropped the "ace/lib/ace" directory into my app's directory, included:

我正在尝试将ace 编辑器添加到我的应用程序中。我从 github 下载它,将“ace/lib/ace”目录放到我的应用程序目录中,包括:

<script src="ace/lib/ace/ace.js" type="text/javascript" charset="utf-8"></script>"

in my body tag and:

在我的身体标签中:

editor = ace.edit "editor"

in my script tag. I've tried to load the page in Chrome and Firefox and I get "define is not defined" in ace.js:46. The line in ace.js is:

在我的脚本标签中。我尝试在 Chrome 和 Firefox 中加载页面,但在 ace.js:46 中得到“未定义定义”。ace.js 中的一行是:

define(function(require, exports, module) {

Does anyone know why ace is expecting the define() function to exist and why it's not finding it? Here's my source:

有谁知道为什么 ace 期望define() 函数存在以及为什么找不到它?这是我的来源:

<html>
  <body>
    <div id="editor">some text</div>
    <script src="ace/lib/ace/ace.js" type="text/javascript" charset="utf-8"></script>
    <script>
      var editor = ace.edit("editor");
    </script>
  </body>
</html>

采纳答案by Shaurav Garg

If you already have the source, then it is pretty easy to do still. Just go in the directory where you copied all the ace source.

如果您已经拥有源代码,那么仍然很容易做到。只需进入复制所有 ace 源的目录即可。

Then, do:

然后做:

npm install
node Makefile.dryice.js

See the wiki for additional details https://github.com/ajaxorg/ace/wiki/Building-ace

有关其他详细信息,请参阅 wiki https://github.com/ajaxorg/ace/wiki/Building-ace

回答by Mark Fielbig

You are getting this error because the RequireJSJavaScript library has not been included in your page.

您收到此错误是因为RequireJSJavaScript 库尚未包含在您的页面中。

To fix this either use an ace build or include RequireJS in your page.

要解决此问题,请使用 ace 构建或在您的页面中包含 RequireJS。

If you choose to include RequireJS your html fragment will look something like this:

如果您选择包含 RequireJS,您的 html 片段将如下所示:

<!-- Editor will go here -->
<div id="editor"></div>

<!-- Load RequireJS -->
<script src="lib/requirejs/require.js"></script>

<!-- Initialize ace -->
<script>

    // Tell RequireJS where ace is located
    require.config({
        paths: {
            'ace': 'lib/ace'
        }
    });

    // Load the ace module
    require(['ace/ace'], function(ace) {
        // Set up the editor
        var editor = ace.edit('editor');
        editor.setTheme('ace/theme/monokai');
        editor.getSession().setMode('ace/mode/javascript');
        // etc...
    });
</script>

回答by ulu

I hacked it by putting window.define = ace.define;in my DOMload handler.

我通过放入window.define = ace.define;我的 DOMload 处理程序来破解它。

回答by Alexander Patrick

Alternatively you can use a cdn

或者,您可以使用 cdn

And replace

并更换

<script src="/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>

With something like

像这样的东西

<script src="http://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js" type="text/javascript" charset="utf-8"></script>