Changing body styles in vue router

前端 未结 7 1421
滥情空心
滥情空心 2021-02-01 18:27

I\'m using Vue router with two pages:

let routes = [
    {
        path: \'/\',
        component: require(\'./components/HomeView.vue\')
    },
    {
        pa         


        
7条回答
  •  迷失自我
    2021-02-01 19:26

    watch: {
      $route: {
        handler (to, from) {
          const body = document.getElementsByTagName('body')[0];
          if (from !== undefined) {
            body.classList.remove('page--' + from.name.toLowerCase());
          }
          body.classList.add('page--' + to.name.toLowerCase());
        },
        immediate: true,
      }
    },
    

    Another fairly simple solution, add it to your base App.vue file. The to.name can be replaced with to.meta.class or similar for something more specific. This is a nice do it once and it works forever type solution though.

提交回复
热议问题