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");
},
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"
}
}
}
The reason why your Main
controller is created twice is because its view is created twice.
manifest.json
and looks at the /sap.ui5/rootView/viewName
value to create the assigned view ("CompleteSurvey.view.Main"
).Component.js
), sees that the current hash or pattern is ""
, and creates the corresponding view which is the "Main"
view again.The current best practice is to have a separate root view. You can keep the Main
for the ""
pattern, but avoid using the same view again as a root view.
For further references, take a look at the tutorial Navigation and Routing and this answer.
Another typical reason is the Component itself being created twice with the same name
.
Component.js
or Component-preload.js
is fetched only once but not twice or both at the same time.sap/ui/core/ComponentSupport
, sap/ui/core/ComponentContainer
, sap.ui.core.Component#createComponent
, sap/ui/core/Component.create
, sap/ui/core/Component.load
, sap.ui.component
sap/ui/core/Core.createComponent
/sap.ui5/routing/targets
with "type": "Component"
. Review in your project whether the Component is being accidentally created twice with the same name
.