Home  >  Q&A  >  body text

javascript - Bottom component, why does the Vue bound class text not change color? The following code

I put
data () {

return {
  isActive: 'a'
}

}, If the 'a' in
is replaced with 'b', the published words will change color

<template lang="html">
<p class="footer">

<p class="footer-box">
  <router-link :class="{'active': isActive === 'a'}" @click="select('a')" to="/" class="item">首页</router-link>
  <router-link :class="{'active': isActive === 'b'}" @click="select('b')" to="/create" class="item">发表</router-link>
  <router-link :class="{'active': isActive === 'c'}" @click="select('c')" to="/message" class="item">消息</router-link>
  <router-link :class="{'active': isActive === 'd'}" @click="select('d')" to="/user" class="item">我的</router-link>
</p>

</p>
</template>

<script>
export default {
data () {

return {
  isActive: 'a'
}

},
methods: {

select (value) {
  this.isActive = value
}

}
}
</script>

<style lang="less">
.footer {

position: fixed;
left: 0;
bottom: 0;
height: 50px;
width: 100%;
background-color: #fff;
border-top: 1px solid #bbb;
.footer-box {
  display: flex;
  height: 50px;
  width: 100%;
  line-height: 40px;
  .item {
    flex: 1;
    text-align: center;
  }
}

}
.active {

color: #41B883;

}
</style>

巴扎黑巴扎黑2665 days ago1271

reply all(5)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-05 10:42:18

    What you wrote is too complicated. This can be done by writing a linkActiveClass when configuring the route. It is recommended to read the documentation of vue-router.
    The configuration can be written in the main.js defined route.
    const router = new VueRouter({

    linkActiveClass: "active",    //设置点击状态的class
    mode: 'hash',
    hashbang:true,
    routes:routerConfig

    })

    Then add the .active{color: #41B883;} style to your .vue style

    router-link is written like this<router-link to="/" class="item">Homepage</router-link>

    reply
    0
  • 習慣沉默

    習慣沉默2017-07-05 10:42:18

    It should be enough to replace it with @click.native. Of course, the method above is better

    reply
    0
  • 阿神

    阿神2017-07-05 10:42:18

    Replace the click event with @click.native="select('a')";
    When writing this kind of navigation, I usually use this method, v-for
    <ul>

           <li v-for="(item,index) in liData" :class="    {active:$route.matched[0].path==item.label}" ><router-link :to="item.label">{{item.name}}</router-link></li>

    </ul>
    data(){

    return {
    
      liData:[
        {name:"首页",label:"/home"},
        {name:"设计器",label:"/designer"},
        {name:"任务管理",label:"/taskmanger"},
        {name:"节点管理",label:"/node"}
      ]
      },
    这里的active的变化就是根据地址栏中变化而变化,这样前进后退都不会有问题

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 10:42:18

        <router-link :to="item.url" active-class="act-bar" tag="li" exact>
         
        </router-link>

    You can define the selected style .act-bar{}

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-07-05 10:42:18

    <p class="footer-box">
       <router-link to="/">
          首页
       </router-link>
    </p>
    .router-link-active{
        color: #41B883;
    }

    Just do this. The color set in .router-link-active is the router-link you are currently clicking on. The style color after activation can also be set to other styles, and then other router-links will be restored to the default. Style

    reply
    0
  • Cancelreply