M
M
myskypesla2018-04-17 15:41:08
Vue.js
myskypesla, 2018-04-17 15:41:08

How to pass a value to another component?

There is a Preloader component and a Header component.
When you click on the button in the Preloader component, it changes show to !show and this show should be passed to the Header component, which will only show at the moment v-if="!show".
thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2018-04-17
@myskypesla

Something like this:

<template>
  <preloader @show="show = $event"></preloader>
  <header v-if="!show"></header>
</template>

export default {
  data() {
    return {
      show: true
    }
  }
}

In the preloader:
<template>
  <button @click="handler"></button>
</template>
export default {
  data() {
    return {
      show: false
    }
  },
  methods: {
    handler() {
      this.show = !this.show
      this.$emit('show', this.show)
    }
  }
}

Although there is a duplication here, therefore it is more correct that the variable that is responsible for the display is not inside the component, but outside.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question