The App component is the base component that renders all of the child components in the application. We'll briefly review the App component's code to gain a better understanding of Vue. Going forward, we'll describe the role each remaining component plays, but only review sections of the corresponding code. The contents of the App component file, located at /src/components/App.js, are shown as follows:
import BalancesBar from './BalancesBar/BalancesBar.js';
import ChartsTab from './ChartsTab/ChartsTab.js';
import TransactionsTab from './TransactionsTab/TransactionsTab.js';
/**
* This component is the entry point for the application. It contains the
* header, tabs, and content.
*/
export default {
name: 'app',
components: {
BalancesBar,
ChartsTab,
TransactionsTab
},
data() {
return {
balances: $store.state.balances,
activeTab: 0
};
},
methods: {
// Any time a transaction is added, edited, or removed, we need to
// ensure the balance is updated:
onTransactionChange() {
$store.recalculateBalances();
this.balances = $store.state.balances;
},
// When the "Charts" tab is activated, this ensures that the charts
// get automatically updated:
onTabClick(event) {
this.activeTab = +event.target.dataset.tab;
}
},
template: `
<div>
<div class="appHeader uk-background-primary uk-flex uk-flex-middle">
<h2 class="uk-light uk-margin-remove-bottom uk-margin-left">
Cook the Books
</h2>
</div>
<div class="uk-position-relative">
<ul uk-tab class="uk-margin-small-bottom uk-margin-top">
<li class="uk-margin-small-left">
<a href="#" data-tab="0" @click="onTabClick">Transactions</a>
</li>
<li>
<a href="#" data-tab="1" @click="onTabClick">Charts</a>
</li>
</ul>
<balances-bar
:balances="balances"
:onTransactionChange="onTransactionChange">
</balances-bar>
<ul class="uk-switcher">
<li>
<transactions-tab :onTransactionChange="onTransactionChange">
</transactions-tab>
</li>
<li>
<charts-tab :isActive="this.activeTab === 1"></charts-tab>
</li>
</ul>
</div>
</div>
`
};
We use the components property to specify the other Vue components we'll render in the template for the App component. The data() function, which returns the local state, is used to keep track of balances and which tab is active (TRANSACTIONS or CHARTS). The methods property contains two functions: onTransactionChange() and onTabClick(). The onTransactionChange() function calls $store.recalculateBalances() and updates balances in local state if a change is made to a transaction record. The onTabClick() function changes the value of activeTab in the local state to the data-tab attribute of the clicked tab. Finally, the template property contains the markup used to render the component.