Sapui5 如何检索视图';带应用程序前缀的ID?

Sapui5 如何检索视图';带应用程序前缀的ID?,sapui5,Sapui5,要检索控件的前缀ID,可以使用视图的方法CreateID。但是,在类sap.ui.core.core中没有这样的方法。那么我如何知道行中的viewID呢 sap.ui.getCore().byId(viewID)核心对于加载到HTML文档中的所有应用程序都是通用的。特定应用程序的特定视图可以具有不同的ID,具体取决于您已加载的不同视图的数量。CreateID方法创建一个以相关视图ID为前缀的ID是有意义的,但是视图ID不是前缀,而是自动生成的 因此,要获取当前视图的ID,只需在其控制器中执行t

要检索控件的前缀ID,可以使用视图的方法CreateID。但是,在类sap.ui.core.core中没有这样的方法。那么我如何知道行中的viewID呢


sap.ui.getCore().byId(viewID)
核心对于加载到HTML文档中的所有应用程序都是通用的。特定应用程序的特定视图可以具有不同的ID,具体取决于您已加载的不同视图的数量。CreateID方法创建一个以相关视图ID为前缀的ID是有意义的,但是视图ID不是前缀,而是自动生成的

因此,要获取当前视图的ID,只需在其控制器中执行
this.getView()
,并从返回的对象获取ID

如果您想知道与正在可视化/加载的视图不同的视图的ID,那么需要浏览DOM中的所有视图,并设计自己的方法来识别它

但是,我不建议您从另一个视图的控制器修改视图。每个视图只能由其控制器中的逻辑修改,而不能由其他控制器修改,以维护MVC体系结构的原则

编辑:

如何访问您的组件:
  • 如果使用Push.js脚本绑定应用程序执行的上下文,则使用
    this.getOwnerComponent()
  • 如果没有,则需要使用
    sap.ui.getCore().getComponent('componentID')
  • 现在的问题是如何知道组件ID。我在这里看到两个选项,为组件提供一个ID,或者将自动生成的组件ID映射到核心变量中

    如何检索特定组件: 选项1:定义组件ID

    每当您初始化它时,通常在index.html文件中,您可以这样做:

         <script>
            sap.ui.getCore().attachInit(function() {
                sap.ui.component({
                    id: "myComponentID",
                    async: true,
                    name: "myNamespace", // The namespace you defined in the bootstrap tag and points to the folder where the Component.js file is stored
                    manifestFirst: true
                }).then(function(oNewComponent){
                    new sap.m.Shell({
                        app: new sap.ui.core.ComponentContainer({
                            component: oNewComponent,
                            height : "100%"
                        })
                    }).placeAt("content");
                })
            });
        </script>
    
    sap.ui.getCore().getComponent(sap.ui.getCore()._data_oLoadedComponents.myCustomComponentId)
    
    sap.ui.getCore().getComponent(sap.ui.getCore()._data_oLoadedComponents.myCustomComponentId).getModel('modelName')
    
    因此,现在您可以访问从映射变量获取自动生成ID的任何组件,如下所示:

         <script>
            sap.ui.getCore().attachInit(function() {
                sap.ui.component({
                    id: "myComponentID",
                    async: true,
                    name: "myNamespace", // The namespace you defined in the bootstrap tag and points to the folder where the Component.js file is stored
                    manifestFirst: true
                }).then(function(oNewComponent){
                    new sap.m.Shell({
                        app: new sap.ui.core.ComponentContainer({
                            component: oNewComponent,
                            height : "100%"
                        })
                    }).placeAt("content");
                })
            });
        </script>
    
    sap.ui.getCore().getComponent(sap.ui.getCore()._data_oLoadedComponents.myCustomComponentId)
    
    sap.ui.getCore().getComponent(sap.ui.getCore()._data_oLoadedComponents.myCustomComponentId).getModel('modelName')
    
    也可以这样访问模型:

         <script>
            sap.ui.getCore().attachInit(function() {
                sap.ui.component({
                    id: "myComponentID",
                    async: true,
                    name: "myNamespace", // The namespace you defined in the bootstrap tag and points to the folder where the Component.js file is stored
                    manifestFirst: true
                }).then(function(oNewComponent){
                    new sap.m.Shell({
                        app: new sap.ui.core.ComponentContainer({
                            component: oNewComponent,
                            height : "100%"
                        })
                    }).placeAt("content");
                })
            });
        </script>
    
    sap.ui.getCore().getComponent(sap.ui.getCore()._data_oLoadedComponents.myCustomComponentId)
    
    sap.ui.getCore().getComponent(sap.ui.getCore()._data_oLoadedComponents.myCustomComponentId).getModel('modelName')
    

    实际上,我想从Push.js模型访问我的odata模型。据我所知,要获得它,我需要先访问视图。或者有没有一种方法可以在不访问视图的情况下实现这一点?这一切都取决于注册模型的位置。如果在视图中注册模型,则是,您需要自己访问该视图。但通常在组件级别注册OData模型。例如,当您在manifest.json文件中配置它时,模型将在组件级别注册。因此,您只需要从核心检索所需的组件,然后使用
    getModel('modelName')
    。我已经编辑了我的答案,给你们一些如何做的想法。第一个选项似乎在移动设备上不起作用,因为组件的ID仍然是自动生成的。第二个很好用。非常感谢。很高兴听到这个!!如果答案解决了您的问题,请记住向上投票并选择它作为有效答案,这样它可以帮助有相同问题的人。它在堆栈溢出中声明