SAPUI5在按下按钮时打开链接 [英] SAPUI5 Open link on Button press
问题描述
我有一个SAPUI5应用程序,如果我单击一个按钮,想打开一个链接.
I have a SAPUI5 application and want to open a link if I click on a button.
我从OData调用中获得了特定链接的值,并且正常的< Link>
控件可以正常使用远程数据路径.
I get the value of the specific link from an OData call and a normal <Link>
control is working fine with the remote data path.
<Link href="{oData>/Url}" target="_blank" text="Click me" />
尝试了几种使用< Button>
来打开网站的方法,但是没有一种对我有用.
Tried several ways to open the website with a <Button>
instead, but none of them were working for me.
我试图在其中放置一个按钮.
I tried to put a Button inside of it.
<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>
但是我遇到了以上错误.
But I'm getting the above error.
尝试使用HTML链接,但无法处理远程数据的路径:
Tried using an HTML link, but it can't deal with the path of the remote data:
<html:a href="{oData>/Url}" target="_blank"><Button text="Open Website"/></html:a>
然后我在使用按钮和 window.open
函数时尝试使用 onPress
事件处理程序:
Then I tried to use the onPress
event handler when using a Button and the window.open
function:
<Button text="Open Website" press=".onPress" />
{ // In the Controller
onPress: function () {
window.open("{oData>/Url}","_blank");
},
}
但是Controller也无法处理导致相同无效URL的远程数据的路径.
But the Controller also can't deal with the path of the remote data resulting in the same invalid URL.
I also read from URLHelper
and tried this sample, but I'm unable to add the "value" attribute to the Button.
推荐答案
<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));
},
}
有关更多信息,请参见:
For more information, see:
- 传递参数"部分.
- API参考:树液.m.URLHelper.redirect .
或者通过直接在XML中直接请求模块(自UI5 1.69开始)):
Alternatively without a controller by requiring the module directly in XML (since 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)"
/>
这篇关于SAPUI5在按下按钮时打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!