问题描述
我有一个简单的小部件:
I've got a simple widget:
define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/dom-construct"],
function(declare, WidgetBase, domConstruct){
return declare("gijit.workflow.debug.combi", [WidgetBase], {
startup: function(){
alert("started");
},
buildRendering: function(){
var container = domConstruct.create("div", {innerHTML:"test"}, this.domNode, "first");
var rad1 = domConstruct.create("input", {type:"radio"}, container, "first");
var rad1 = domConstruct.create("input", {type:"radio"}, container, "last");
}
});
});
和一个简单的加载页面:
and a simple loading page:
<!DOCTYPE html>
<html >
<head>
<link rel="stylesheet" href="../../../css/tcs-style-dijit.css" />
<script>dojoConfig = {parseOnLoad: false}</script>
<script src="../../../js/dojo/dojo.js" data-dojo-config="async: true"></script>
<!-- <script src="../../../js/gijit/workflow/debug/combi.js"></script> -->
<script>
require(["dojo/dom", "gijit/workflow/debug/combi", "dojo/parser",
"dojo/_base/window"], function(dom, combi, parser, window) {
var widget = new combi();
widget.placeAt(window.body());
// widget.startup();
});
</script>
</head>
<body id="dd" class="tcs">
</body>
</html>
但是,我收到以下错误:
However, I get the following error:
组件返回的故障代码:0x80004003(NS_ERROR_INVALID_POINTER)[nsIDOMHTMLBodyElement.appendChild]
域> http:// 域:端口 / js / dojo / dojo .js
第15行
Component returned failure code: 0x80004003 (NS_ERROR_INVALID_POINTER) [nsIDOMHTMLBodyElement.appendChild]domain">http://domain: port/js/dojo/dojo.jsLine 15
我将错误隔离到我试图将小部件放在HTML中的位置:
I have isolated the error to the point where I try to place the widget in the HTML:
widget.placeAt(window.body());
我无法弄明白导致该问题的原因,并希望有些帮助
I am unable to figure out what is causing the issue, and would appreciate some help
推荐答案
您调用 window.body()
的时候,DOM尚未被解析,因此返回 undefined
。需要插件作为最后一个模块加载:
At the moment you invoke window.body()
the DOM has not been parsed yet and it therefore returns undefined
. Require dojo/domReady!
plugin as the last module loading:
require([
"dojo/dom",
"gijit/workflow/debug/combi",
"dojo/parser",
"dojo/_base/window",
"dojo/domReady!" // ==> wait for the DOM to be ready
], function(dom, combi, parser, win) {
var widget = new combi();
widget.placeAt(win.body());
widget.startup();
});
请注意,作为最佳做法,我建议不要隐藏窗口
对象通过将 dojo / _base / window
模块分配到本地窗口
变量中, code> win 。
Please note, that as a best practice I would recommend not to hide window
object by assigning dojo/_base/window
module into a local window
variable, use win
instead.
这篇关于我无法在页面中放置Dojo小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!