javascript - 如何在 native 博览会中深度链接到特定屏幕

标签 javascript react-native expo deep-linking

我正在使用以下代码创建链接 URL:

Linking.makeUrl('lobby/', {
  roomId: params.roomId
})

输出以下内容:exp://192.168.0.31:19000/--/lobby/?roomId=1585512451
这在本地工作正常,但似乎只打开到我的应用程序的主页。

我在我的 app.js 中定义了屏幕
const Stack = createStackNavigator();

function App() {  
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen 
          name="Home" 
          component={HomeScreen} 
        />
        <Stack.Screen 
          name="Lobby"
          component={LobbyScreen} 
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

我是否需要使用某种均匀的监听器重定向到大厅屏幕,还是应该将路径传递到 makeUrl映射到屏幕?

最佳答案

是的,您需要监听 url 事件。请参阅 Expo 文档中有关深度链接的部分。 https://docs.expo.io/versions/latest/workflow/linking/#handling-links-into-your-app

请注意,在 expo 配置中需要额外的配置选项才能在不同的设备上工作。有关这方面的所有说明,请参阅他们的文档。

从提供的链接引用

Handling links into your app
There are two ways to handle URLs that open your app.

1. If the app is already open, the app is foregrounded and a Linking event is fired
You can handle these events with Linking.addEventListener('url', callback).

2. If the app is not already open, it is opened and the url is passed in as the initialURL
You can handle these events with Linking.getInitialURL -- it returns a Promise that resolves to the url, if there is one.

从他们的示例代码中:
let redirectUrl = Linking.makeUrl('path/into/app', { hello: 'world', goodbye: 'now' });
然后您需要使用事件处理程序处理 URL
_handleUrl = url => {
  this.setState({ url });
  let { path, queryParams } = Linking.parse(url);
  alert(`Linked to app with path: ${path} and data: ${JSON.stringify(queryParams)}`);
};

因为如果您的应用程序打开并单击链接与关闭应用程序时行为不同,您需要 2 个不同的入口点来处理链接。

你的内心 HomeScreen组件你可以这样写:
    componentDidMount() {
        // handle an initial url on app opening
        Linking.getInitialURL().then(urlRedirect)
    }

在你的应用程序的某个地方,也许在 app.js为应用程序内部的新网址放置一个处理程序。
function urlRedirect(url) {
    if(!url) return;
    // parse and redirect to new url
    let { path, queryParams } = Linking.parse(url);
    console.log(`Linked to app with path: ${path} and data: ${JSON.stringify(queryParams)}`);
    this.navigation.replace(path, queryParams);
}

// listen for new url events coming from Expo
Linking.addEventListener('url', event => {
    urlRedirect(event.url);
});

关于javascript - 如何在 native 博览会中深度链接到特定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60920124/

相关文章:

javascript - 我如何在javascript中创建一个搜索栏

javascript - 如何将对象数组从输入框传递到 JavaScript 中的单独数组中

javascript - 当光标隐藏在其他 HTML 元素后面时如何触发 mouseenter 事件?

javascript - 使用 Linkedin javascript 身份验证时本地主机来源无效

react-native - 将 Region latitudeDelta/longitudeDelta 转换成近似的 zoomLevel

android - 如何在 React Native 中实现 'FrameLayout' 组件?

javascript - React Native : React Navigation StackNavigator not working. 获取错误 : "undefined is not an object (evaluating ' this. props.navigation.navigate')”

android - 使用 expo build 读取 ECONNRESET

react-native - Expo React Native - FaceID 在 Expo Client 中不可用

react-native - 有没有办法使用 Expo Image Picker 获取图像文件的原始名称?