TIL a clean way to add classes based on the page in Vue
POSTED ON:
TAGS: clean-code arrays vue
In Vue:
The Vue Version #
<!-- The long way -->
<vue-component :class="{
'classOnlyOnThesePages' :
$route.name === 'Home' || $route.name === 'Gallery' || $route.name === 'Profile'
}" />
<!-- The short way -->
<vue-component :class="{
'classOnlyOnThesePages' : ['Home', 'Gallery', 'Profile'].includes($route.name)
}" />
Pretty cool, huh? Super readable!
It pretty much is just this:
const addClassesToThisPage = ( ['Home', 'Gallery', 'Profile'].include($route.name))
Related TILs
Tagged: clean-code