You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SAP UI5中不使用全局变量传递sRoute值的实现方法

How to Pass Route Value Without Global Variables in SAPUI5

Hey there! Let's solve this problem of accessing the sRoute value in your _onWeiterButtonPress function without relying on global variables. Here are two clean, SAPUI5-native approaches you can use:

Approach 1: Store as a Controller Private Property

This is the simplest solution for sharing values within the same controller. We'll store the route value as a private property on the controller instance (using an underscore prefix to indicate it's internal):

  1. Update your _onRouteMatched function to save the route value:
_onRouteMatched: function(oEvent) { 
  this._sRoute = oEvent.getParameter("arguments").Route; // Assign to controller private property
}
  1. Access the value directly in _onWeiterButtonPress:
_onWeiterButtonPress: function(oEvent) { 
  var oRouter = this._getRouter(); 
  var AufNr = this.getView().byId("IptAufNr").getValue(); 
  var sCurrentRoute = this._sRoute; // Retrieve the stored route value
  
  // Now use sCurrentRoute to determine which tile was clicked
  if (sCurrentRoute === "someTileRoute") {
    // Your tile-specific logic here
  }
}

Since SAPUI5 controllers are instantiated per view, this private property won't leak to other parts of your app and stays neatly encapsulated within the controller.

Approach 2: Use a View JSON Model (Great for View Binding)

If you ever need to use the route value in your view (e.g., displaying it to the user) or want a more reactive approach, store the value in a dedicated JSON model:

  1. Initialize the model in your onInit function:
onInit: function() { 
  sap.ui.core.UIComponent.getRouterFor(this).getRoute("CoObject").attachMatched(this._onRouteMatched, this); 
  // Create a small model to hold route information
  this.getView().setModel(new sap.ui.model.json.JSONModel({ currentRoute: "" }), "routeModel");
}
  1. Update the model when the route matches:
_onRouteMatched: function(oEvent) { 
  var sRoute = oEvent.getParameter("arguments").Route;
  this.getView().getModel("routeModel").setProperty("/currentRoute", sRoute);
}
  1. Fetch the value in your button press handler:
_onWeiterButtonPress: function(oEvent) { 
  var oRouter = this._getRouter(); 
  var AufNr = this.getView().byId("IptAufNr").getValue(); 
  var sCurrentRoute = this.getView().getModel("routeModel").getProperty("/currentRoute");
  
  // Implement your tile-specific logic here
}

This approach is ideal if you need to bind the route value to UI elements (e.g., <Text text="{routeModel>/currentRoute}" />), as the model will automatically update the UI when the route changes.

Both methods avoid global variables and follow SAPUI5 best practices for encapsulation and data management. Pick the one that best fits your use case!

内容的提问来源于stack exchange,提问作者BurgGartenKind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:00:18