javascript - 在 react-native 中打开路由中未指定的 DeepLink

标签 javascript react-native react-navigation

如果我考虑一个例子

https://app.abc.com/login 

这会在我的应用程序中打开登录页面。但是如果链接是这样的

https://app.abc.com/loginUser //This link is a route in web app

这不会在应用程序中打开登录页面,因为路径未在路由中定义

现在的要求是,每当用户点击第二个链接时,即使这样它也应该在 App 而不是在 web 中打开登录组件。 IE。同一组件的多个路由,或者我可以为此类路由打开一个通用组件吗?我们可以在 React-Native 中实现吗?

最佳答案

这非常简单,只需浏览 React-Navigation 的文档即可

import { NavigationActions } from 'react-navigation' 
    const previousGetActionForPathAndParams = MyAPP.router.getActionForPathAndParams;

    Object.assign(MyApp.router, {
      getActionForPathAndParams(path) {
        console.log("path in navigation", path)
        if (
          path === 'loginUser'
        ) {
          // returns a profile navigate action for /my/custom/path
          return NavigationActions.navigate({
            routeName: 'Login',
          });
        }
        // else {
        //   console.log("you have landed in an unknown space")
        // }
        return previousGetActionForPathAndParams(path);
      },
    });

将此代码插入您的导航文件中,您就可以使用 React-Navigation

在以前的版本中,我们可以根据 here 为特定组件提供多个路径来做到这一点

感谢@DoğancanArabacı 的宝贵评论,这曾经是一个方便的解决方案

关于javascript - 在 react-native 中打开路由中未指定的 DeepLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57000501/

相关文章:

react-native - React Native 如何做最小/最大宽度

reactjs - React Native - React Navigation 在每个页面上显示页眉和页脚

android - 错误 : null is not an object (evaluating 'C.State' ) <unknown> index. android.bundle

javascript - 如何在 Angular 页面上显示非 $scope 变量?

javascript - 获取从今天开始的星期几

javascript - jQuery FullCalendar 中未显示的事件

javascript - React Native onPress 事件延迟

javascript - 更改所有具有 id= 值的元素

typescript - React Native - 不变违规 : Module AppRegistry is not a registered callable module (calling runApplication)

javascript - 为什么我的 this.props.navigation.setParams 不起作用?