SAPUI5 在按下按钮时打开 link

SAPUI5 Open link on Button press

我有一个 SAPUI5 应用程序,我想在单击按钮时打开 link。

我从 OData 调用中获取了特定 link 的值,并且正常的 <Link> 控件在远程数据路径下工作正常。

<Link href="{oData>/Url}" target="_blank" text="Click me" />

尝试了几种使用 <Button> 打开网站的方法,但 none 对我有用。

我试着在里面放了一个按钮。

<Link target="_blank" href="{oData>/Url}" text="Click me">
  <Button text="Open Website"/> <!-- Error: Cannot add direct child without default aggregation defined for control sap.m.Link -->
</Link>

但是我遇到了上面的错误。

尝试使用 HTML link,但无法处理远程数据的路径:

<html:a href="{oData>/Url}" target="_blank"><Button text="Open Website"/></html:a>

然后我尝试在使用 Button 和 window.open 函数时使用 onPress 事件处理程序:

<Button text="Open Website" press=".onPress" />
{ // In the Controller
  onPress: function () {
    window.open("{oData>/Url}","_blank");
  },
}

但是Controller也无法处理远程数据的路径导致同样无效URL.

我也阅读了 URLHelper 并尝试了 this sample,但我无法将“值”属性添加到按钮。

<Button text="Open Website" press=".openUrl(${oData>/Url}, true)" />
{ // In the Controller
  openUrl: function(url, newTab) {
    // Require the URLHelper and open the URL in a new window or tab (same as _blank):
    sap.ui.require([ "sap/m/library" ], ({URLHelper}) => URLHelper.redirect(url, newTab));
  },
} 

有关详细信息,请参阅:


或者没有控制器 requiring the module directly in XML(自 UI5 1.69 起):

<Button xmlns="sap.m"
  xmlns:core="sap.ui.core"
  core:require="{ sapMLib: 'sap/m/library' }" 
  text="Open Website"
  press="sapMLib.URLHelper.redirect(${oData>/Url}, true)"
/>