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

242
Views
How to write an Event in Composition API (Vue3)?

I am currently having this event in a vue3 component:

<template>
    <div class="hamburger-toggle" @click="toggle" :class="{ nav__open: isActive }">
</template>

<script>
export default {
  name: "navbar",
  data() {
    return {
      isActive: true
    }
  },
  methods: {
    toggle: function () {
      this.isActive = !this.isActive;
    }
  }
}
</script>

How would I write this in the composition api?

Failed attempt:

<script>
export default {
  props: {
    isActive: false
  },
  setup(props) {
    function toggle(props) {
      props.isActive = !props.isActive;
    }
  }
}
</script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try like this (in composition API you use ref or reactive instead data function, and you return your functions/reactive data):

<template>
    <div class="hamburger-toggle" @click="toggle" :class="{ nav__open: isActive }">
</template>

<script>
import { ref } from "@vue/reactivity";
export default {
  name: "navbar",
  setup() {
    const isActive = ref(true)
    const toggle = () => {
      isActive.value = !isActive.value
    }
  return { isActive, toggle }
}
</script>
about 4 years ago · Juan Pablo Isaza Report

0

Also, it will work with a little less code:

<template>
  <div @click="isActive = !isActive"> ... </div>
</template>

<script setup>
import { ref } from "vue";
const isActive = ref(true)
</script>

you can read here about the <script setup>

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!