如何在没有嵌套导航的情况下进行导航

时间:2018-12-13 07:08:32

标签: javascript reactjs react-native react-navigation

我对本机路由器使用react-navigation stackNavigator。简单的路径是LoginRouter -> DashboardRouter -> AddressRouter,这里是地图树。

// LoginRouter.js
const LoginRouter = createStackNavigator(
  {
    Login: Login,
    Register: Register,
    Dashboard: DashboardRouter
  },
  {
    initialRouteName: "Login"
  }
);

// DashboardRouter.js
const TabNavigator = createBottomTabNavigator(
  {
    MessageRouter: MessageRouter,
    AddressRouter: AddressRouter,
  },
  {
    ...
  }
);

// AddressRouter.js
const AddressRouter = createStackNavigator(
  {
    // these all navigator will become a nested navigation
    Address: Address,
    UserAdd: UserAdd,
    UserGroup: UserGroup
  },
  {
    initialRouteName: "Address"
  }
);

enter image description here

如果我想让AddressRouter在没有嵌套导航的情况下进行导航,如何更改此代码?

PS

将AddressRouter提取到LoginRouter时,导航工作正常。

// LoginRouter.js
const LoginRouter = createStackNavigator(
  {
    Login: Login,
    Register: Register,
    Dashboard: DashboardRouter,
    Address: Address,
    UserAdd: UserAdd,
    UserGroup: UserGroup
  },
  {
    initialRouteName: "Login"
  }
);

// DashboardRouter.js
const TabNavigator = createBottomTabNavigator(
  {
    MessageRouter: MessageRouter,
    AddressRouter: AddressRouter,
  },
  {
    ...
  }
);

1 个答案:

答案 0 :(得分:1)

如果我的问题正确,那么您可以做的是创建另一个导航器作为根导航器(另一个StackNavigatorSwitchNavigator),并包含您的LoginRouterAddressRouter在那里。

也许是这样的

const rootNavigator = createSwitchNavigator(
    {
      LoginRouter: {
        screen: LoginRouter,

      },
      AddressRouter: {
        screen: AddressRouter,

      }
    },
    {
      headerMode: "none",
      mode: "modal",
      initialRouteName: "LoginRouter"
    }
  );

这只是一个示例,可让您了解如何实现路由器。