Home  >  Q&A  >  body text

In Vue, how do I get the emitted value from a child component and then use it in the parent component's return data?

How to get the title value passed by the Topbar component and use the value in the data() return part? I tried adding a method to pass the value, but unfortunately, although I was able to console log the value in the parent file, it didn't work. I'm still a beginner in vue js so I'd like some help on this issue. Thanks!

<template>
    <div>
        <Topbar :tabs='tabs' @pass-data="getTabTitle"/>
        
    </div>
</template>
<script>
    import Topbar from "../components/Navigation/Topbar.vue";

    export default {
        name: "Progress",
        components: {
            Topbar,
    },  
        
        data() {
            return {
                title:'',//have the emitted value here
                tabs: [  
                    {
                        link:'',
                        name: "Sec 1",
                        dropdown: false,
                        dropdownTabs:[]
                    },
                    {
                        link:'',
                        name: "Sec 2",
                        dropdown: false,
                        dropdownTabs:[]
                    }
                ]
            }
    },
          methods: {
            getTabTitle(title) {
                console.log(title)
                this.title =title
        }
    },
    }
</script>

Top bar template

<template>
  <ul class="navbar-nav">
                    
       <li @click="onClick(tab.name)"
                        v-for="(tab, index) in tabs.filter((tab)=> tab.dropdown===false)" :key="index">
                        {{tab.name}}
                    </li>
      <DropdownMenu v-for="(tab, index) in tabs.filter((tab)=> tab.dropdown===true)" :key="index"
                        :tab="tab" />

  </ul>
</template>

<script>
    import DropdownMenu from "./DropdownMenu.vue";

    export default {
        name: "Topbar",
        props: {
            tabs: Array
        },
        components: {
            DropdownMenu,

    },
     methods: {
        onClick(tabName) {
            this.$emit('pass-data',tabName)
        }
    }   

    }
</script>

P粉523625080P粉523625080304 days ago392

reply all(1)I'll reply

  • P粉667649253

    P粉6676492532023-12-20 19:41:16

    Try a code snippet like below (pass all tabs to child and loop there):

    const app = Vue.createApp({
      data() {
        return {
          title:'',
          tabs: [  
            {link:'', name: "Sec 1", dropdown: false, dropdownTabs:[]},
            {link:'', name: "Sec 2", dropdown: false, dropdownTabs:[]}
          ]
        }
      },
      methods: {
        getTabTitle(title) {
          console.log(title)
          this.title =title
        }
      }
    })
    app.component('topbar', {
      template: `
        <div v-for="(tab, i) in tabs" :key="i">
          <button @click="send(tab.name)">{{ tab.name }}</button>
        </div>
      `,
      props: ['tabs'],
      methods: {
        send(title) {
          this.$emit('passData', title)
        }
      }
    })
    app.mount('#demo')
    <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
    <div id="demo">
      <b>{{ title }}</b>
      <topbar :tabs='tabs' @pass-data="getTabTitle"></topbar>
    </div>

    reply
    0
  • Cancelreply