Why Is My Main Controller Being Called Twice?

Ada*_*kus 5 sapui5

All other routings are fine, but for some reason the main controller is being called twice. Why would this happen?

onInit: function() {
  var oRouter = this.getOwnerComponent().getRouter();
  oRouter.getRoute("main").attachMatched(this._onRouteMatched, this);
  this.getView().setModel(new JSONModel({
    Jobs: []
  }), "job");
},
Run Code Online (Sandbox Code Playgroud)

Is this down to the routing config?

"rootView": {
  "viewName": "CompleteSurvey.view.Main",
  "type": "XML"
},
"routing": {
  "routes": [{
    "name": "main",
    "pattern": "",
    "target": ["main"]
  }],
  "config": {
    "routerClass": "sap.m.routing.Router",
    "viewType": "XML",
    "viewPath": "CompleteSurvey.view",
    "controlId": "app",
    "controlAggregation": "pages"
  },
  "targets": {
    "main": {
      "viewName": "Main"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Bog*_*ann 7

你的Main控制器被创建两次的原因是因为它的视图被创建了两次。

  1. 您的组件获取manifest.json并查看rootView以创建分配的视图 ( "CompleteSurvey.view.Main")。
  2. 你的路由器被初始化,看到当前的哈希/模式是"",并"Main"再次创建相应的视图。

当前的最佳实践是拥有一个单独的根视图。你可以保持Main""模式,但要避免再次使用相同的看法为根视图。


如需进一步参考,请查看教程导航和路由以及此答案

  • 感谢您的准确回答。对我帮助很大。 (2认同)