我添加了 react-navigation-drawer 用于在我的应用中实现抽屉导航。我创建了一个名为PrimaryNav.js的文件,并在其中添加了所有导航代码。
import Login from './components/Login';
import Employee from './pages/Employee';
import { createAppContainer,SafeAreaView, } from 'react-navigation'
import { createDrawerNavigator, DrawerItems } from 'react-navigation-drawer';
import React from 'react';
const Primary_Nav = createDrawerNavigator({
Login: {
screen: Login,
navigationOptions: {
drawerLabel: () => null
}
},
Home_kitchen: {
screen: Home_kitchen,
navigationOptions: {
drawerLabel: "Home"
}
},
Employee: {
screen: Employee,
navigationOptions:{
drawerLabel:"Employee",
}
},
},{
initialRouteName:'Login',
drawerPosition: 'left',
drawerType: "slide",
}
});
const PrimaryNav = createAppContainer(Primary_Nav);
export default PrimaryNav;
类似上面的东西。我已经在App.js
中调用了此文件,我面临的问题是我需要根据用户的角色来设置一个抽屉项目。因此,如果用户角色是收银员,则他应该看不到所有菜单。
所有页面都正确显示在抽屉菜单中,但问题是我应该如何在我的应用中明智地管理菜单角色并根据用户角色更改菜单? >
答案 0 :(得分:1)
嗨,我看到了您的问题,并正在努力为您提供帮助。
我已经为抽屉组件定制了设计。
-首先,您可以为抽屉设计创建一个额外的文件,例如DrawerComponent.js
并在您创建抽屉式导航器的代码中导入
import DrawerComponent from "./DrawerComponent";
const Primary_Nav = createDrawerNavigator(
{
Login: {
screen: Login,
navigationOptions: {
drawerLabel: () => null
}
},
Home_kitchen: {
screen: Home_kitchen,
navigationOptions: {
drawerLabel: "Home"
}
},
Employee: {
screen: Employee,
navigationOptions: {
drawerLabel: "Employee"
}
}
},
{
initialRouteName: "Login",
drawerPosition: "left",
drawerType: "slide",
contentComponent: DrawerComponent // i added this DrawerComponent
}
);
const PrimaryNav = createAppContainer(Primary_Nav);
export default PrimaryNav;
现在在DrawerComponent.js
import React, { Component } from "react";
import { Text, View, TouchableOpacity } from "react-native";
export default class DrawerComponent extends Component {
constructor(props) {
super(props);
this.state = {
role: 1 // i used 1 for cashier and 0 for chef
};
}
render() {
const { role } = this.state;
const { navigation } = this.props;
return (
<View style={{ flex: 1, paddingVertical: 40, paddingHorizontal: 20 }}>
<TouchableOpacity
style={{ margin: 20 }}
onPress={() => navigation.navigate("Home_kitchen")}
>
<Text>Home</Text>
</TouchableOpacity>
{role ? (
<TouchableOpacity
style={{ margin: 20 }}
onPress={() => navigation.navigate("Employee")}
>
<Text>Employee</Text>
</TouchableOpacity>
) : null}
</View>
);
}
}
如果您将角色更改为0,则在 Drawer Navigator (抽屉导航器)中禁用Employee选项卡。我为条件指定了三元运算符。您可以根据需要进行修改。希望对您有帮助。