Dynamically inserting a CSS class to navbar based on route

前端 未结 3 1248
囚心锁ツ
囚心锁ツ 2021-01-02 11:37

Vue 2 and Vue-Router 2.

I\'m trying to change the color of my app\'s navbar based on which route is visited. Here\'s what I have:

main.js:

im         


        
3条回答
  •  被撕碎了的回忆
    2021-01-02 12:08

    There are multiple ways you can do it. I myself have different kind of headers depending of the page I am on.

    One simple way can be to have some check on which route you are and depending on route change this variable. You can put a watch on $route, and whenever it changes, you can decide the value of colorNav depending on current route. Code will be something like:

    
    

    Another way to do it can be have this variable in some centralised state or vuex store and change this from each component's mounted block depending on requirement.

提交回复
热议问题