Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
Cannot read properties of undefined Nuxt.js

I am trying to call the showMenu method on the click event based on the value passed on "open" variable.

But I m getting cannot read properties of undefined while, open is a defined variable.

I have tried to console log something on the click events it works, but whenever I try to interact with the open variable which is defined to the best of my knowledge I am getting the undefined error message.

<template>
  <nav>
  <div class="header-one">
    <div class="header">
      <div class="logo">Logo</div>
      <div class="navigation" id="nav">
        <nuxt-link to="/">Home</nuxt-link>
        <nuxt-link to="/">About</nuxt-link>
        <nuxt-link to="/">Login</nuxt-link>
        <nuxt-link to="/">Logout</nuxt-link>
        <nuxt-link to="/">Profile</nuxt-link>
      </div>
      **<div class="burger" id="burger" @click="showMenu">**
        <div></div>
        <div></div>
        <div></div>
      </div>
    </div>
  </div>
    <MobileMenu v-if="this.open"/>
  </nav>
</template>

<script>
import MobileMenu from "~/components/MobileMenu";

export default {
  name: "HeaderMenu",
  MobileMenu,

data: function(){
    return{
      **open: false,**
    }
},

  methods:{
    showMenu:() =>{
      **this.open = !this.open**
    }
  }
}
</script>

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This happen because your showMenu function is an arrow function wich change the scope of this.

Here this refer to the scope of the function instead of the components

Use a classic function instead

Here is an example

new Vue({
  el: '#app',
  data: () => {
    return {
      open: false
    }
  },
  
  methods: {
    toggleOpen1(){
      this.open = !this.open
    },
    toggleOpen2: () => {
      this.open = !this.open
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div>Open : {{ open }}</div>
  <button @click="toggleOpen1">Toggle open 1</button>
  <button @click="toggleOpen2">Toggle open 2</button>
</div>

As you can see, the toggleOpen1 (classic function) methods work whereas the toggleOpen2 (arrow function), doesn't work.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!