I'm fairly new to Vue, which is why I can't ignore my feeling that I'm doing something wrong.
I have an independent component inside a view component, which has to emit data to other components.
The project has the following components: App
, Navbar
, Home
, Statistics
, Table
and Search
.
The Search
component is used inside the Home
component. It contains an <input>
to let the user input his account name.
The Table
component is used inside the Statistics
component. It shows a table based on the input from the Search
component.
+-- App
| +-- Navbar
| +-- Home
| +-- ---- Search
| +-- Statistics
| +-- ---- Table
My main App.vue
contains the router-view
:
<template>
<div id="app">
<Navbar></Navbar>
<div class="container">
<router-view></router-view>
</div>
</div>
</template>
Two of them are main view components, which have a fixed route: Home
& Statistics
.
The routes (router.js
) are the following:
export default new Router({
routes: [{
path: '/',
name: 'home',
component: Home
},
{
path: '/statistics',
name: 'statistics',
component: Statistics
}
]
})
The Navbar
component contains the router-link
s to the view components:
<template>
<div id="navbar">
<nav>
<ul>
<li class="active"><router-link to="/">Home</router-link></li>
<li><router-link to="/statistics">Statistics</router-link></li>
</ul>
</nav>
</div>
</template>
Now: What is the right way to get the data from the independent child component Search
to my other independent child component Table
?
The logical way would be Search
-> Home
-> App
-> Statistics
-> Table
.
I already tried to use an event bus with $emit
and $on
, but it seems like the data can't connect to each other with the router inbetween.
Right now, I have a const in my main.js
to store this global user data:
export const store = new Vue({
data: {
username: ''
}
})
which I import everywhere I need it, but I highly doubt that this is the right way to do it.
I also thought about using props for my Table
component, but I would need to pass my data from App
to Statistics
to Table
which would work, but feels like a lot of boilerplate. Also: I need to get the username to the App
before I can do that anyways, which is what I don't know how to do proberly.
Thanks in advance.