The idea is to have 'isActive' boolean array in the Vue instance in order to update it every time the navbar gets loaded, i.e using the current page name taken from the URL as an index in the array and set it's value to true; and 'onbeforeunload' setting it to false.
Navbar.vue
<template>
<div class="navbar">
<nav>
<ul>
<li v-bind:class="{ active: isActive['login'] }">
<a href="/login">Log in</a>
</li>
<li v-bind:class="{ active: isActive['signup'] }">
<a href="/signup">Sign up</a>
</li>
</ul>
</nav>
</div>
</template>
<script>
import Vue from 'Vue'
var navbarOpts = {
name: 'navbar',
data {
return {
isActive: []
}
}
})
module.exports = navbar
function pageName(pathname) {} // Returns no-spaced string
var currentPage = pageName(window.location.pathname)
if (currentPage !== '') { // If not in home page
navbar.data().isActive[currentPage] = true
window.onbeforeunload = function () {
navbar.data().isActive[currentPage] = false
}
}
</script>
Here I would reactively update the current page index (example: 'login') in the array to true, and as new pages get visited (example: 'signup') they will get added as indexes and set to true; and set to false before unloading resources.
Maybe I should use Vue.set(vm.isActive, currentPage, false)
so a new property is reactively added to vm.isActive if it doesn't exist or updated. The problem is there is no point in me creating a Vue instance since I can't export it. And if I modify the navbarOpts and export it like I did, navbarOpts gets created everytime Navbar.vue is rendered and everything it holded gets lost.