Changing body styles in vue router

前端 未结 7 1436
滥情空心
滥情空心 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:08

    If the class is view specific, may be this will help

    methods: {
      toggleBodyClass(addRemoveClass, className) {
        const el = document.body;
    
        if (addRemoveClass === 'addClass') {
          el.classList.add(className);
        } else {
          el.classList.remove(className);
        }
      },
    },
    mounted() {
      this.toggleBodyClass('addClass', 'mb-0');
    },
    destroyed() {
      this.toggleBodyClass('removeClass', 'mb-0');
    },
    

    Move the methods section to a mixin and then the code can be DRY.

提交回复
热议问题