我承认这和我以前的一个问题很相似,但并不完全一样。由于我最终使用的是ajax请求,所以我决定将opal添加到我的堆栈中(除非您知道一种更简单的方法),转而使用它提供的Document.ready?函数。
我的功能现在看起来如下:
def render_component component,mount_point
React.render React.create_element(component), Element.find[mount_point][0]
end我这样叫它:
Document.ready? do
render_component LoginForm, '#view_port'
end问题是,在这之前,它是有效的,现在,它不是,我得到了一个Uncaught NoMethodError: undefined method '[]' for nil错误,它是有效的,然后它突然开始这样做。
错误是由于某种原因,Element.find[mount_point]正在返回nil,而我不知道原因。它应该在dom加载之后运行,因此挂载点'#view_port'应该存在;正如您在这里看到的那样,它肯定存在于dom中:
<body>
<section id="main">
<section id="view_port">
</section>
</section>
<div class="hiddendiv common"></div>
</body>我甚至尝试过用window.addEventListener("load", function () {}包装它,但没有效果。
为什么现在不行了?我错过了什么?
发布于 2015-08-18 10:54:18
好的,问题是render_component函数中的render_component是在Document.ready?之前调用的,所以返回的是零。为什么这是我不知道,它应该有效,但它不是,所以你去。
另一个值得注意的问题(稍微相关)是,尝试Element['#view_port'][0]获取本机对象会导致错误Uncaught TypeError: b.toLowerCase is not a function,因此您不能使用Jquery获取本机dom元素,这意味着您只能使用document.getElementById('view_port')。
https://stackoverflow.com/questions/32069742
复制相似问题