【问题标题】:SAPUI5 Open link on Button pressSAPUI5在按钮按下时打开链接
【发布时间】:2021-04-03 20:03:47
【问题描述】:

我有一个 SAPUI5 应用程序,如果我点击一个按钮,我想打开一个链接。

我从 OData 调用中获取特定链接的值,并且普通的 <Link> 控件与远程数据路径正常工作。

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

尝试了几种使用&lt;Button&gt; 打开网站的方法,但没有一种方法适合我。

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

<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链接,但无法处理远程数据的路径:

<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,但我无法将“值”属性添加到按钮。

【问题讨论】:

    标签: javascript xml sapui5


    【解决方案1】:
    <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)"
    />
    

    【讨论】:

    • 第二种方法对我来说效果很好,非常感谢。
    • @Xuki 很高兴听到。但我想知道为什么第一种方法对你的情况不起作用。还是它也有效?
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2014-01-27
    • 2015-03-09
    相关资源
    最近更新 更多