Module SAPUI5开关元件

Module SAPUI5开关元件,module,components,sapui5,Module,Components,Sapui5,在组件之间切换的最佳方式是什么?例如,我有一个仪表板组件和两个子组件。如何从仪表板切换到此组件?我找到了方法“oComponentContainer.setContainer(“componentId”);” 这是切换到其他组件的正确方法吗 我也做过类似的事情 创建组件容器 this._oContainer = new ComponentContainer(this.createId("CONTAINTER"), { handleValidation: true });

在组件之间切换的最佳方式是什么?例如,我有一个仪表板组件和两个子组件。如何从仪表板切换到此组件?我找到了方法“oComponentContainer.setContainer(“componentId”);”

这是切换到其他组件的正确方法吗

我也做过类似的事情

创建组件容器

this._oContainer = new ComponentContainer(this.createId("CONTAINTER"), {
            handleValidation: true
});
...
开关元件

var oComponent = this.getComponentById(sId, sRelativePathToComponent);
this._oContainer.setComponent(oComponent);
按id检索现有组件或从路径创建新组件,插入依赖项(如模型或事件总线)

getComponentById: function(sId, sCompName) {
    var oComponent = sap.ui.getCore().getComponent(sId);
    if (!oComponent) {
        oComponent = sap.ui.component({
            name: sCompName,
            id: sId,
            componentData: {
                model: this._oModel,
                eventBus: this._oComponent.getEventBus()
            }
        });
    }
    return oComponent;
}

我的解决方案与Jasper_07的类似

按事件切换到组件时:

...onPressTile: function(oControlEvent){
        var comp = this.oView.oController.getComponent("dashboardCh", oControlEvent.getSource().data("componentName"));
        sap.ui.getCore().getEventBus().publish("dashboardCh", oControlEvent.getSource().data("componentName"));
    }...
组件处理程序功能:

/*
     * Component handler
     */
    getComponent: function(channel, componentName){
        var eventBus = sap.ui.getCore().getEventBus();

        if(!this.oView.oController.checkComponentExists(channel, componentName)){
            //create component
            var newComp = new sap.ui.getCore().createComponent({    
                name: componentName,
                id: componentName,
                height: "100%"
            });

            //subscripe component
            this.oView.oController.subscripeToEventBus(newComp, channel);
        };

    },

    checkComponentExists: function(_channel, _componentName){
        var componentExists = false;

        if(sap.ui.getCore().getEventBus()._mChannels.dashboardCh != undefined){
            if(_componentName in sap.ui.getCore().getEventBus()._mChannels.dashboardCh.mEventRegistry){
                componentExists = true;
            }
        }

        return componentExists;
    }, 

    subscripeToEventBus: function(newComp, _channel){
        sap.ui.getCore().getEventBus().subscribe(_channel, newComp.getId(), function(){
            sap.ui.getCore().getElementById("compContainer").setComponent(newComp.getId());
        }, this);
    }