如何通过使用Vue保持CSS样式

时间:2019-01-15 10:50:02

标签: javascript html css vuejs2 vue-router

我正在学习vue,并尝试使用vue建立网站。我在导航栏中设置了两个导航选项卡。

这是路由器配置文件:

import Vue from 'vue'
import Router from 'vue-router'
import Management from '@/components/Management'
import Execution from '@/components/Execution'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: { name: 'Management'}
    },
    {
      path: '/management/list',
      name: 'Management',
      component: Management,
    },
    {
      path: '/execution/list',
      name: 'Execution',
      component: Execution
    }
  ]
})

router.beforeEach((to, from, next) => {
  next();
})

export default router

和App.vue:

<template>
  <div class="container">
    <div id="header">
        <ul class="nav-ul">
            <router-link tag="li" :class="['nav-li', {active: show === 'management'}]" v-on:click="changeShow('management')" :to="{name: 'Management'}"><span>case management</span></router-link>
            <router-link tag="li" :class="['nav-li', {active: show === 'execution'}]" v-on:click="changeShow('execution')" :to="{name: 'Execution'}"><span>case execution</span></router-link>
        </ul>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  data: function(){
    return {
      show: 'management'
    }
  },
  computed: {
    changeShow: function (show) {
        this.show = show;
    }
  },
  watch: {
    '$route'(to, from) {
      if (to.name) {
        this.show = to.name.toLowerCase()
      }
    }
  }
}
</script>

<style>
  .container {
    height: 100%;
  }
  #header {
    height: 50px;
    width: 100%;
    background-image: linear-gradient(to bottom,#FF3300 0,#ff6600 20%);
  }
  .nav-ul,.nav-li 
  {
    margin: 0px;
    padding: 0px;
    list-style: none;
  }
  .nav-ul 
  {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .nav-li {
    width: 100px; 
    height: 38px;
    text-align: center;
    padding-top: 12px;
    font-size: 16px;
  }
  .nav-li:hover {
    background-color: #FFCC00;
    cursor:pointer;
  }
  .nav-li.active {
    background-color: #FFCC00;
  }
</style>

当我单击execution选项卡时,预计它的背景色将更改为#FFCC00(选项卡类为nav-li active),浏览器网址为/execution/list。但是,刷新页面后,management选项卡的类为nav-li active,背景色为#FFCC00,而URL仍为/execution/list。为什么management选项卡的类别变为nav-li active,而execution选项卡的类别却变为nav-li

1 个答案:

答案 0 :(得分:1)

您正在$route上运行mounted属性,当路线发生变化时,它可以正常工作。但是当您刷新时,路线不会改变,因此下划线监视永远不会触发。 创建一个show函数,并在其中检查路由名称,并相应地修改mounted(){ this.show = this.$route.name.toLowerCase(); } 数据。这样的事情应该起作用:

SELECT c.* 
FROM tblClaim c
WHERE c.StatusID = 2 AND
      DAY(c.IncidentDate) = DAY(GETDATE()) AND
      MONTH(c.IncidentDate) = MONTH(GETDATE());