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>
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({
})
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>
It should be enough to replace it with @click.native. Of course, the method above is better
Replace the click event with @click.native="select('a')";
When writing this kind of navigation, I usually use this method, v-for
<ul>
</ul>
data(){
You can define the selected style .act-bar{}
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