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>
Try a code snippet like below (pass all tabs to child and loop there):