简体   繁体   中英

SAPUI5: Navigate to dynamically created view

I want to dynamically create a view based on an existing view and then add the necessary routing/target. Currently I use the following code to achieve this:

var viewName = name + "View";
var targetName = name + "Target";
var routeName = name + "Route";

if (this.getRouter().getRoute(routeName) === undefined) {        
    console.log("create view");

    var view = sap.ui.view(({ viewName: "demo.view.Test", type: sap.ui.core.mvc.ViewType.XML }));
    this.getRouter().getTargets().getViews().setView(viewName, view);

    console.log("create target");

    this.getRouter().getTargets().addTarget(targetName, {
        viewName: viewName,
        viewPath: "demo.view",
        viewLevel: 2,
        parent: "base",
        controlId: "mainContainer",
        controlAggregation: "pages",
        title: name
    });

    console.log("create route");

    this.getRouter().addRoute({
        name: routeName,
        pattern: name,
        target: targetName
    });
}; 

However if I run this code (eg name = "Test2") and then try to navigate to the generated route, I get an error saying that the file containing the view could not be found (eg Test2.view.xml).

Is there a way to use this dynamically created view an a target without having a view file?

Update

The idea of creating an alias for a view to support multiple instances was taken from the UI5 documentation: https://openui5.hana.ondemand.com/#/api/sap.m.routing.Targets/constructor (refer to the viewName in the constructor)

Since UI5 1.56 there is a new method sap.ui.core.mvc.View.create .

You could try something like this:

sap.ui.require(['sap/ui/core/mvc/View'],
   function(View){
     View.create({
       viewName: "demo.view.ViewToGetCloned",
       viewId: "demo.view.NameOfNewView",
       type: sap.ui.core.mvc.ViewType.XML
       }).then(function(oView) {
          ...    
       }.bind(this));
}.bind(this));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM