首页 > 解决方案 > 如何将重定向网址嵌入网站?(在 React Native 中)

问题描述

我想将此文本集成到我的应用程序“ https://www.google.com ”中。当我们单击它时,我希望移动浏览器打开并显示该网站。这个怎么做?我是新手,任何帮助将不胜感激。

<TouchableOpacity>
<View>
    <Text style={{color: 'grey', fontSize: 17, fontFamily:'Arial', fontStyle: 'bold', textAlign: 'center', 
    marginTop: 6, marginLeft: 25, marginBottom: 20}}> 
                https://www.google.com/
    </Text>
  </View>
</TouchableOpacity>

标签: javascriptreact-nativeweburltext

解决方案


根据官方React Native Linking 文档

如果您使用托管的 expo-cli 工作流程,请参阅Expo 文档中的链接指南以获取适当的替代方案。

这个页面说:

通常,WebBrowser 是一个更好的选择,因为它是您应用程序中的一种模式,用户可以轻松地关闭它并返回您的应用程序。

所以你也可以使用这个:

<Text style={{
  color: 'grey',
  fontSize: 17,
  fontFamily:'Arial',
  fontStyle: 'bold',
  textAlign: 'center', 
  marginTop: 6,
  marginLeft: 25,
  marginBottom: 20
 }}
 onPress = {() => {WebBrowser.openBrowserAsync('https://google.com')}}> 
</Text>

推荐阅读