gpt4 book ai didi

javascript - Opal RB 中未初始化的常量 Object::Element

转载 作者:数据小太阳 更新时间:2023-10-29 07:56:26 27 4
gpt4 key购买 nike

尝试使用 Opal/JQuery。我的 app.rb 文件如下所示:

require 'opal'
require 'opal-jquery'

class HTMLObject
def initialize

end

def write_to_body

end
end


class HTMLParagraph < HTMLObject
attr_accessor :inner_html
def initialize(text)
@inner_html= text
end

def write_to_body

@body = Element.find("#body")
@body.append(Element("<p>#{@inner_html}"))
end
end

p = HTMLParagraph.new("hello world")
p.write_to_body

我使用站点中的示例将其编译为 app.js。我在我的网络浏览器中使用 index.html 运行它:

<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script src="opal.js" type="text/javascript"></script>
<script src="opal-jquery.min.js" type="text/javascript"></script>
<script src="opal-parser.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<title></title>
</head>
<body>

</body>
</html>

当我打开页面时,我没有看到任何东西。控制台显示此错误跟踪:

Uncaught NameError: uninitialized constant Object::Element opal.js:1531
def.$backtrace.backtrace opal.js:1531
def.$raise opal.js:1279
def.$const_missing opal.js:575
Opal.cm opal.js:255
def.$write_to_body app.js:44
(anonymous function) app.js:51
(anonymous function)

JS 输出文件如下所示:

 function(__opal) {
var p = nil, _a, _b, self = __opal.top, __scope = __opal, nil = __opal.nil, $mm = __opal.mm, __breaker = __opal.breaker, __slice = __opal.slice, __klass = __opal.klass;
(function(__base, __super){
function HTMLObject() {};
HTMLObject = __klass(__base, __super, "HTMLObject", HTMLObject);

var def = HTMLObject.prototype, __scope = HTMLObject._scope;

def.$initialize = function() {

return nil;
};

def.$write_to_body = function() {

return nil;
};

return nil;
})(self, null);
(function(__base, __super){
function HTMLParagraph() {};
HTMLParagraph = __klass(__base, __super, "HTMLParagraph", HTMLParagraph);

var def = HTMLParagraph.prototype, __scope = HTMLParagraph._scope;
def.inner_html = def.body = nil;

def.$inner_html = function() {

return this.inner_html
},
def['$inner_html='] = function(val) {

return this.inner_html = val
}, nil;

def.$initialize = function(text) {

return this.inner_html = text;
};

def.$write_to_body = function() {
var _a, _b, _c;
this.body = ((_a = ((_b = __scope.Element) == null ? __opal.cm("Element") : _b)).$find || $mm('find')).call(_a, "#body");
return ((_b = this.body).$append || $mm('append')).call(_b, ((_c = this).$Element || $mm('Element')).call(_c, "<p>" + (this.inner_html)));
};

return nil;
})(self, ((_a = __scope.HTMLObject) == null ? __opal.cm("HTMLObject") : _a));
p = ((_a = ((_b = __scope.HTMLParagraph) == null ? __opal.cm("HTMLParagraph") : _b)).$new || $mm('new')).call(_a, "hello world");
return ((_b = p).$write_to_body || $mm('write_to_body')).call(_b);
})(Opal);

有什么想法吗?

最佳答案

尝试将 opalopal-jquery 直接放在您的 html 中,并将要求放在 app.rb 之外,您可以获取它们来自 http://cdnjs.com .

否则我想查看编译后的 app.js(您可以将其放在 gist 中)。

关于javascript - Opal RB 中未初始化的常量 Object::Element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16629602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com