问题
Can I know the difference and performance when I use
var myControl = this.getView().byId("myIDhere");
var myControl = this.byId("myIDhere");
var myControl = sap.ui.getCore().byId("myIDhere");
Which of three is best to use to perform operations on control when I use XML views in UI5 apps?
回答1:
this.getView().byId(...)
is equivalent tothis.byId(...)
. Take a look at the source code to see whatbyId
does:// sap.ui.core.mvc.Controller Controller.prototype.byId = function(sId) { return this.oView ? this.oView.byId(sId) : undefined; };
As you can see,
this.byId
is just a shortcut forthis.getView().byId
. They both can be used to access controls defined in the view. E.g.:this.byId("myPanel");
sap.ui.getCore().byId(...)
API, on the other hand, awaits a fully concatenated global ID which is why you cannot simply exchangethis.byId
withsap.ui.getCore().byId
if the target control is a view descendant.sap.ui.getCore().byId("__xmlview0--myPanel"); // <-- Please don't do that!
More about IDs:
- Stable IDs: All You Need to Know
- For the sake of completeness: How to Access Elements from XML Fragment by ID
来源:https://stackoverflow.com/questions/48639302/difference-between-this-getview-byid-this-byid-and-sap-ui-getcore-byid