Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript sapui5组件设置有时显示,有时未定义(相同代码)_Javascript_Asynchronous_Sapui5 - Fatal编程技术网

Javascript sapui5组件设置有时显示,有时未定义(相同代码)

Javascript sapui5组件设置有时显示,有时未定义(相同代码),javascript,asynchronous,sapui5,Javascript,Asynchronous,Sapui5,我想为不同的用途使用相同的SAPUI5组件 我想通过设置区分不同的用途,如下所示 var oComponentContainer = new sap.ui.core.ComponentContainer( { url : "resources/components/clientSelector", name : "components.clientSelector", id :

我想为不同的用途使用相同的SAPUI5组件

我想通过设置区分不同的用途,如下所示

var oComponentContainer = new sap.ui.core.ComponentContainer( {
                    url : "resources/components/clientSelector",
                    name : "components.clientSelector",
                    id : "components.clientSelector",
                    propagateModel : true,
                    settings : {
                        bPopupView : true,
                        iOffset : $(document).height() - 300
                    }
                });
有时当我在console.log my component.oContainer中显示时,它会完全按照预期显示,有时它根本不显示任何内容,它会显示未定义的


这可能与我的javascript是异步的有关吗?

我找到了问题和答案。SAPUI5在创建容器之前创建并实例化组件。根据组件创建的速度(包括视图和控制器初始化),设置设置时,此代码可能已经完成

我还找到了一种解决方法:我现在使用componentData属性,而不是设置,它的工作方式很有魅力:

  var oComponent = sap.ui.getCore().createComponent( {
        url: "resources/components/clientSelector",
        name: "components.clientSelector",
        id: "components.clientSelector",
        componentData: {
          test: "xx"
        }
      });
      var oComponentContainer = new sap.ui.core.ComponentContainer( {
        component: oComponent
      });

我可以从第一个视图的初始化中访问oComponent.getComponentData(),并返回测试数据。

“有时”,你的意思是什么时候?@SunilBN完全相同的代码,如果我按control+F5,它可以工作大约50%的时间,它是否能够在线运行代码段?或者你可以把完整的代码放在这里。因为我在当前代码中没有看到component.oContainer变量snippet@SunilBN如果你想知道的话,我找到了答案并贴在下面。谢谢你的关注