注意vue-router嵌套路由的問題:子路由組件中的class樣式被覆蓋,當需要用到路由嵌套時,clas命名時注意不要相同。
例子:
子路由a組件中:
<template> <div class="children">我是a組件</div> </template> <style scoped> .children{ font-size: 16px; color: red; text-align: center; margin-top: 100px; } </style>
本頁面standard組件:
<template> <div class="standard"> <headerBack title="嵌套路由"></headerBack> <div> <div class="tab"> <router-link to="/standard/a"> <div class="children">我是a組件</div> </router-link> <router-link to="/standard/b"> <div class="children">我是b組件</div> </router-link> <router-link to="/standard/c"> <div class="children">我是c組件</div> </router-link> </div> <router-view/> </div> </div> </template> <script> import headerBack from "./../../components/header"; export default { components: { headerBack }, }; </script> <style lang="less" scoped> .standard { .tab { display: flex; justify-items: center; justify-content: space-around; } .children { border-radius: 5px; text-align: center; font-size: 16px; margin: 20px 0; background-color: #1989fa; padding: 10px; color: #fff; } } </style>
這樣子a組件的children樣式沒生效:樣式效果為standard組件中的children樣式,結果如下:
原因是a組件的children樣式被覆蓋了:
當子路由a組件中改變class名時且命名不相同時:
<template> <div class="a">我是a組件</div> </template> <style scoped> .a{ font-size: 16px; color: red; text-align: center; margin-top: 100px; } </style>
效果為:(正常)
當然也可以給樣式權重優先級來解決。