http://plnkr.co/edit/bSm4nYJ0TmUqylidxkqt?p=preview
我正在尝试使用 requirejs 注入(inject)对 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:未定义,消息:“活着”}
最佳答案
我通过使用以下代码得到了它:
将 /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
关于javascript - 无法使用 requirejs 获取对 ACE 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18338839/