Is it necessary to close the navigation drawer after selecting an item?

Currently, I'm working on a project for my University where I am trying to incorporate a feature that will automatically close the navigation drawer whenever any of its items are clicked. However, I am facing some challenges figuring out how to achieve this.


    <template>
      <div id="navigation-mobile">
        <Searchbar class="search"/>
        <ul v-for="item in tabs"
                :key="item.path"
                active-class
                @click="$router.push(item.path)"
        >
          <li>{{item.name}}</li>
        </ul>
        <div class="mobile-footer">
          <ul>
            <li>About us</li>
            <li>Contact us</li>
          </ul>
        </div>
      </div>
    </template>
  

I have also included a snippet from App.vue below, which showcases a section of the nav-drawer:


    <template>
      <v-app id="app">
        <NavBarMobile v-if="mobileView"/>
        <div class="content" :class="{'open': showNav}">
          <div style="height:20px"></div>
          <div id="navigation-icon" v-if="mobileView"
          @click="showNav = !showNav">
            <v-icon medium dark>menu</v-icon>
          </div>
          <NavBar v-if="!mobileView"></NavBar>
          <v-content class="pa-0" transition="slide-x-transition"></v-content>
          <Footer v-if="!mobileView"></Footer>
          <router-view></router-view>
        </div>
      </v-app>
    </template>
  

This is the progress I've made so far. I believe using a @click event would be the most efficient solution, but I'm unsure if it can be implemented due to existing uses within the code. As programming isn't my strongest suit, I would greatly appreciate any advice or suggestions to overcome this hurdle.

You can access the codepen link here: https://codesandbox.io/s/gameshelf-0209-jack-forked-zobe5

To view the component mentioned above, navigate to NavBarMobile.vue

Thank you for taking the time to review this post and offer assistance!

Answer №1

To achieve this, one method you can use is "watching" the $route object directly within your App.vue:

export default {
  // ...
  watch: {
    $route(to, from) {
      this.showNav = false
    }
  }
}

The watch property on the Vue instance consists of functions that monitor changes to variables with matching names and trigger actions upon change.

For further information on watchers and computed properties, visit: https://v2.vuejs.org/v2/guide/computed.html

UPDATE: Additional insights on responding to Router changes can be found here: https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes

Answer №2

One way to make it happen is by triggering an event from the child component.

  • Start by connecting the click event to a method that will manage your logic, like this:
<ul v-for="item in tabs" :key="item.path" active-class @click="redirect(item.path)">
  • Next, include a method in your script to emit a custom event from your NavBarMobile component:
methods: {
        redirect(path) {
            this.$router.push(path)
            this.$emit('change', false)
        }
    },
  • Lastly, listen for this event from the parent component:
<NavBarMobile v-if="mobileView" @change="showNav = $event"/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing Typography in Material UI with Custom Breakpoints in React CustomThemes

Currently, I am utilizing material UI and React to develop a single-page application (SPA). However, I have encountered an issue with ensuring that my pages are responsive for smaller screen sizes. To address this, I have been manually adding fontSize: { x ...

Using discord.js within an HTML environment can add a whole new level of

I'm in the process of creating a dashboard for discord.js, however, I am facing difficulties using the discord.js library and connecting it to the client. Below is my JavaScript code (The project utilizes node.js with express for sending an HTML file ...

Checking CORS permissions with a pre-flight OPTIONS request

During development, I implement a middleware called cors using the following syntax: app.use(cors({origin: 'http://localhost:8100'})); However, I have noticed that for every route request, two requests are being made as shown in the image below ...

Retrieve information from a website and transfer it to a Google spreadsheet

Despite the wealth of information on this topic, I have yet to find a solution that works for me. My goal is to transfer variables obtained from another website to Google Spreadsheet. These are the variables: var a = parseInt($('table.thinline:eq(4) ...

Tips for removing unnecessary debugging code from JavaScript when compiling or minifying your code

Back in the day, I would always include tons of debug code in my JavaScript app. However, I'm now searching for a method that will eliminate debug code during the compilation/minification phase. Does the world of JavaScript have something similar to ...

Display a concealed div when an ng-click event occurs within an ng-repeat loop

$scope.editPostComment = false; Whenever I click on the button, it displays the textarea in all the repeated items instead of just the clicked div! <div class="commentBox" ng-show = "editPostComment"> <textarea name="editor2" ...

AngularFire and Ionic - There is no data being sent to the server from the form

I am using Ionic and AngularFire to build my app, but I have encountered a new issue: the form data is empty! Only the fields in my Firebase database are visible. How can I retrieve the user-entered form data from the Firebase database? Here is a screensh ...

Code snippet for a click event in JavaScript or jQuery

I initially wrote the code in JavaScript, but if someone has a better solution in jQuery, I'm open to it. Here's the scenario: I have multiple questions with corresponding answers. I want to be able to click on a question and have its answer dis ...

SQL query using Ajax

I'm currently working on an Ajax call using a query string, but I've hit a roadblock midway through. My goal is to update a SQL table with the JavaScript sortable function. After an item has been moved up or down, I want to pass it through Ajax a ...

Performing asynchronous operations in React with axios using loops

On the backend, I have a socket set up to handle continuous requests from the server. Now, my goal is to send requests to the backend API continuously until a stop button is clicked. Using a loop for this task is considered bad practice as it never checks ...

Harnessing the Power of JSON in Rails to Enhance Your jQuery UI Autocomplete Form

In the context of a rails application, I have been exploring different methods to fetch data from a JSON file. Despite my efforts, I have not been able to find a solution that suits my requirements. The static data I have is quite extensive, consisting of ...

The Angular Material Datepicker lacks any selected date

I am currently in the process of developing a web application using Angular and incorporating Angular Material for certain UI components. I have encountered an issue that I am unable to resolve. When attempting to use the datepicker as outlined on https:// ...

Performing a test on API GET Request with Playwright

I've been attempting to verify the GET status using this particular piece of code. Regrettably, I keep encountering an error message stating "apiRequestContext.get: connect ECONNREFUSED ::1:8080". If anyone has any insights or suggestions on how to re ...

Can one manipulate SVG programmatically?

Looking to develop a unique conveyor belt animation that shifts items on the conveyer as you scroll down, then reverses when scrolling up. I discovered an example that's close to what I need, but instead of moving automatically, it should be triggered ...

Tips on saving the background image URL value into a variable

How can I save the URL value of a background image in a variable? For example: image: url(images/9XkFhM8tRiuHXZRCKSdm_ny-2.jpg); I store this value as value = images/9XkFhM8tRiuHXZRCKSdm_ny-2.jpg in a variable and then use this variable in an href tag. ...

Tips on how to update the status variable to true depending on the index value

Once I click on the close button, the value immediately changes to "Fruit." How can I achieve this? For instance: Apple close Grapes close Pineapples close Alternatively, is there a way to set the state of "cancel" to true ...

Issue: Unhandled rejection TypeError: Unable to access properties of an undefined variable (retrieving 'data')

Currently, I am developing applications using a combination of spring boot for the backend and react for the frontend. My goal is to create a form on the client side that can be submitted to save data in the database. After filling out the form and attemp ...

What is the best way to display an image right in the middle of the header?

My project consists of three main files - an HTML, a CSS, and a JS file. I have developed the HTML using the Bootstrap 5.1.3 framework. The issue I am facing pertains to the alignment of the clothing brand logo within the header section. Despite multiple ...

What is the best way to show a datepicker when a button is clicked in vuetify?

Here is the structure of my script: ===========--=========--=============--=============== <template> <v-card max-width="1200" class="mx-auto" > <v-row> <v-col cols="12" sm="6" md="4"> <v-me ...

Guide on Retrieving the Current URL in NodeJS

Currently, I am utilizing express in conjunction with node.js. In my setup, the following code is present: app.get('/callback', async function (req, res) { Whenever a user reaches the callback section of my website, I expect to receive the req ...