What is your preferred way of showing role-based menus and buttons in toolbar and navigation menus?
A typical application may have many toolbars and navigation bars, each relevant to a specific set of users. You can then show/hide the elements based on the user role.
In smaller applications, I simply choose to show or hide the menu items on a single navigation bar. This is easier to develop and to maintain.
An example is below.
We will create a new component called Toolbar.vue
which is included in the app. First, let us look at the template
.
|
|
What we have done so far is pretty simple -
- Used the toolbar component from Vuetify
- Introduced the logo and text on the main toolbar
- We show a simple button on the toolbar based on whether user is logged in. We can as well show a similar menu to what has been used later in the navigation drawer
- Introduced the Vuetify’s navigation drawer component
- Create a menu that lists two sets of items - for user (which gets displayed only if user is logged in) and for admin (again, only if user is logged in and role is admin). The values that represent state of login, role etc. are used from Vuex store, which in turn gets it from the server (the values are set at login, logout and other related events)
- The menus are dynamic - they get the title, icon and the link from
data
section
Let us then include all the states used in the <script>
section.
|
|
script
is pretty simple as well -
- we refer to the various states from store -
isLoggedIn
,isAdmin
,isUser
- component-specific states (e.g. those required to open or close navigation drawer ) are housed within the component itself -
clipped
,navDraw
and the menu items - menu items are picked up from
data
section. They have the title, icon and the link
That is it!
We now have a toolbar and navigation bar with menus that dynamically respond to the current role of a user.