我正在尝试将react-native用于网络应用程序。我想创建一个到不同屏幕的简单链接,但使用 url 而不是元素。
我设法让它与 2 个恼人的空洞一起工作:
window.location.href
和 Linking.getInitialURL()
确实返回完整链接Linking.openURL
仅打开新选项卡,我希望单击时它保持在同一个选项卡中。屏幕 1:
import { Linking, StyleSheet, Text, View } from "react-native";
import React from "react";
import WebView from "react-native-webview";
const Infromation = () => {
const uri = "links";
return (
<Text
onPress={() => {
Linking.openURL("feed");
}}
>
AboutReact
</Text>
);
};
export default Infromation;
const styles = StyleSheet.create({});
该链接会打开 feed 屏幕,但在新选项卡中且 url 尾部未显示:
提要屏幕代码:
import {
KeyboardAvoidingView,
Linking,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import React, { useEffect } from "react";
const Feed = () => {
const url = new URL(window.location.href);
Linking.getInitialURL().then((url) => {
console.log(url);
});
return (
<Text>{url.href}</Text>
);
};
export default Feed;
您可以将第二个参数传递给
openURL()
并将 _self
作为值传递
类似这样的事情
`openURL("your url here", "_self")`