Changing body styles in vue router

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

    You can also do it directly in the router file using the afterEach hook:

    mainRouter.afterEach((to) => {
        if (["dialogs", "snippets"].includes(to.name)) {
            document.body.style.backgroundColor = "#F7F7F7";
            // or document.body.classList.add(className);
        } else {
            document.body.style.backgroundColor = "#FFFFFF";
            // or document.body.classList.remove(className);
        }
    });
    

    afterEach hook documentation

    to is a route object which contains the route name (if named), path, etc. Documentation for all the props

提交回复
热议问题