Hide Navbar when clicking on menu item

I'm struggling with finding a solution to this issue, as I am unsure of how to proceed. The problem I am facing is that when I click on an item, the router-view changes correctly, but the menu remains open. I would like it to close after a click.

This pertains to my navbar component.

    <template>
      <nav class="navbar-main">
        <div class="navbar-button" :class="{'is-active': isActive}" @click="isActive = !isActive">
          <span></span>
          <span></span>
          <span></span>
        </div>
        <div class="nav-menu" :class="{'is-active-menu': isActive}">
          <ul class="primary-menu">
            <li class="nav-menu-item">
              <router-link to="/" exact activeClass="link-active" class="nav-menu--link" tag="a">
                <i class="fas fa-home"></i>Home
              </router-link>
            </li>
            <li class="nav-menu-item">
              <router-link to="/about" activeClass="link-active" class="navbar-item" tag="a">
                <i class="fas fa-user"></i>About
              </router-link>
            <</li>
            <li class="nav-menu-item">
              <router-link to="/resume" activeClass="link-active" class="navbar-item" tag="a">
                <i class="fas fa-address-card"></i>Resume
              </router-link>
            </li>
            <li class="nav-menu-item">
              <router-link to="/portfolio" activeClass="link-active" class="navbar-item">
                <i class="fas fa-briefcase"></i>Portfolio
              </router-link>
            <</li>
          </ul>
          <div class="copy">
            <p>Copyright &copy; {{ new Date().getFullYear() }}</p>
            <p>All rights Reserved</p>
          </div>
        </div>
      </nav>
    </template>

    <script>
    export default {
      data() {
        return {
          isActive: false
        };
      }
    };
    </script>

Answer №1

If you're looking to improve this code, consider implementing some refactoring techniques. One way to do this is by adding a close method:

export default {
  data() {
    return {
      isActive: false,
    };
  },
  methods: {
    close() {
      this.isActive = !this.isActive;
    },
  },
};

After adding the close method, integrate it into your main navigation button:

<div class="navbar-button" :class="{ 'is-active': isActive }" @click="close">

Lastly, include the close method in each of your links (in a simplified version):

<router-link to="/" @click="close">
<router-link to="/about" @click="close">

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

Using React Bootstrap: Passing an array to options in Form.Control

I'm currently utilizing Form.Control to generate a dropdown list and I want the list to be dynamic. Here is my code snippet: render() { return ( <Form.Control as="select" value={this.state.inputval} onChange={this.updateinputval}> ...

Why aren't the divs appearing on the page?

I have developed a JavaScript and PHP page where the script in the PHP page sends data to my SQL database. However, the result is not displayed on my home page. Here is the code snippet: function getVote(question_ID) { var option_ID = document.queryS ...

How can I trigger a PHP function by clicking a button on a PHP page that has already been loaded?

While I've come across a variety of examples, I haven't been able to make them work for the simple task I need to accomplish. The code in these examples seems overly complex compared to what I require. In essence, I have a form that processes dat ...

Extracting JavaScript variable data to PHP using Ajax technology

I've been trying to save latitude and longitude values in PHP variables, but I'm having trouble. Here is the code I'm using. Can anyone please help me figure out why these values are not being stored in PHP variables: Code: <!DOCTYPE h ...

@vue/cli for automated unit testing

I'm currently using @vue/cli version 4.5.15 and looking to write tests for my components. However, when I run the command, yarn test:unit I encounter an error that says: ERROR command "test:unit" does not exist. Do I need to perform additional se ...

"Converting an object to a JSON string using URLSearchParams: A step-by

I am currently working on a piece of code that retrieves all the input types from a form const form = document.querySelector('form'); const data = new URLSearchParams(new FormData(form).entries()); My main concern is how to convert the above ...

Troubleshooting issues with Firebase integration in a Node.js environment

I am currently encountering difficulties implementing Firebase in my Node.js project. Below is the code snippet that I am attempting to execute on Node. var firebase = require("firebase"); var admin = require("firebase-admin"); var serviceAccount = requi ...

Unpacking nested objects using dynamically generated property names in a React state - a guide

Having trouble with using setState and figuring out how to destructure the object with a dynamic property name, denoted by id. The state looks like this after computation: { "inputConfig": { "5d4d684cadf8750f7077c739": { "0": "5d4d ...

Arrange ten objects in each of the four JavaScript arrays in ascending order based on a specific value

model with sample data -> [{ date: '13413413', name: 'asdfasdf', other: 'kjh' }] The getJSON function returns 4 arrays, each containing 10 model objects. array1 = 10 resultsObj sorted by date from newest to o ...

A guide on retrieving and resetting the value of a radio button within a table

I need to create multiple radio buttons within a table using Razor syntax under ASP.NET Core MVC. Each row of the table should have an update and clear link to update the record and clear the selected radio button for that specific row. <table class=&qu ...

Tips on displaying a confirmation message upon a user clicking a checkbox

I am encountering an issue with displaying a confirmation message when a checkbox is clicked. The confirmation box pops up but does not carry out any action, like showing the text within the textbox after the user clicks OK. Any guidance on this matter wou ...

Encountering CORS Error while trying to access Guest App in Virtualbox using Vue, Express, and Axios

I encountered an issue while trying to access my Vue app in Virtualbox from the host, both running on Linux Mint 20. Although I can now reach the login page from my host, I am consistently faced with a CORS error during login attempts: Cross-Origin Request ...

You can only use RSA and DSA public keys during the babel-node installation process

I'm having difficulties with the installation process of babel-node npm i -g babel-node > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53313231363f7e3d3c373613657d67d75716b70793f777b79">[email protected]< ...

Is there a way to create Angular components sourced from an external library using JavaScript?

I'm currently developing an Angular project and leveraging an external component library called NGPrime. This library provides me with a variety of pre-built components, including <p-button> (which I am using in place of a standard <button> ...

Android Webview onLoad function provides incorrect height information

When I load a file:// URL into the webview, issues with height calculation arise. Instead of using WRAP_CONTENT, I calculate the height in javascript during the onPageFinished event in Android. However, about 2 out of 5 times, the javascript reports an inc ...

Troubleshooting issue with jQuery subtraction not functioning as expected

Everything seems to be functioning correctly except for the subtraction operation. function add_culture(ele) { total=parseInt($('#total_price').val()); culture_price=parseInt($('#culture_price').val()); $(& ...

Position a center pivot amidst a collection of 3D shapes within ThreeJS

I am currently working on creating a plugin prototype to allow customization of 3D objects using ThreeJS. You can view my progress so far here: If you've visited the link, you may have noticed that when hovering over the left or right arrow, the obje ...

"Implementing a new dynamic route post-deployment in Nuxt: A step-by-step guide

After successfully deploying my site on AWS using nuxt generate with dynamic routes, I encountered an issue. I wanted to add a new route without having to go through the entire deployment process again. For example: /post/1 /post/2 /post/3 ------- alread ...

Why does Safari browser keep showing NAN instead of a value?

In my quest to calculate the difference between two times in Safari browser, I encountered an issue. The code works perfectly fine in Chrome, but in Safari, it returns NAN. When I run the application for the first time, I save today's date. On subsequ ...

Automatically close one option upon opening another

Displayed below is the HTML code that I have printed with echo: <input id="58" readonly="readonly" class="cell_to_edit" value="Accepted"> <span id="58" class="toggle_status"> <select class="status_change"> <option>Ac ...