服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - 浅谈vue 二级路由嵌套和二级路由高亮问题

浅谈vue 二级路由嵌套和二级路由高亮问题

2021-08-09 16:48bingot JavaScript

这篇文章主要介绍了浅谈vue 二级路由嵌套和二级路由高亮问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

第一层路由我写在app.vue里面。如图所示:

浅谈vue 二级路由嵌套和二级路由高亮问题

footer.vue:

浅谈vue 二级路由嵌套和二级路由高亮问题

二级路由是这样:

浅谈vue 二级路由嵌套和二级路由高亮问题

index.js里面的配置:

浅谈vue 二级路由嵌套和二级路由高亮问题

效果图:

浅谈vue 二级路由嵌套和二级路由高亮问题

效果出来了,又出现新的问题,就是点击二级路由的时候,默认的二级路由高亮不会去掉,如图所示:

浅谈vue 二级路由嵌套和二级路由高亮问题

在网上看到别人用exact方法,即在默认的二级路由里面加上exact,如图所示:

浅谈vue 二级路由嵌套和二级路由高亮问题

补充知识:vue - 子路由-路由嵌套

描述:子路由,也叫路由嵌套,采用在children后跟路由数组来实现,数组里和其他配置路由基本相同,需要配置path和component,然后在相应部分添加<router-view/>来展现子页面信息,相当于嵌入iframe。

浅谈vue 二级路由嵌套和二级路由高亮问题

Home.vue

  1. <template>
  2. <div class="hello">
  3. <h1>{{ msg }}</h1>
  4. <!-- 添加子路由导航 -->
  5. <p>导航 :
  6. <router-link to="/home">首页</router-link> |
  7. <router-link to="/home/one">-子页面1</router-link> |
  8. <router-link to="/home/two">-子页面2</router-link>
  9. </p>
  10. <!-- 子页面展示部分 -->
  11. <router-view/>
  12. </div>
  13. </template>
  14.  
  15. <script>
  16. export default {
  17. name: 'Home',
  18. data () {
  19. return {
  20. msg: 'Home Page!'
  21. }
  22. }
  23. }
  24. </script>
  25.  
  26. <style scoped>
  27. </style>

One.vue /Two.vue

  1. <template>
  2. <div class="hello">
  3. <h1>{{ msg }}</h1>
  4. </div>
  5. </template>
  6.  
  7. <script>
  8. export default {
  9. name: "One",
  10. data() {
  11. return {
  12. msg: "Welcome to One!"
  13. };
  14. }
  15. };
  16. </script>
  17.  
  18. <!-- Add "scoped" attribute to limit CSS to this component only -->
  19. <style scoped>
  20. h1,
  21. h2 {
  22. font-weight: normal;
  23. }
  24. ul {
  25. list-style-type: none;
  26. padding: 0;
  27. }
  28. li {
  29. display: inline-block;
  30. margin: 0 10px;
  31. }
  32. a {
  33. color: #42b983;
  34. }
  35. </style>

index.js

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Home from '@/components/Home'
  4. import One from '@/components/One'
  5. import Two from '@/components/Two'
  6.  
  7. Vue.use(Router)
  8.  
  9. export default new Router({
  10. routes: [
  11. {
  12. path: '/', // 默认页面重定向到主页
  13. redirect: '/home'
  14. },
  15. {
  16. path: '/home', // 主页路由
  17. name: 'Home',
  18. component: Home,
  19. children:[ // 嵌套子路由
  20. {
  21. path:'one', // 子页面1
  22. component:One
  23. },
  24. {
  25. path:'two', // 子页面2
  26. component:Two
  27. },
  28. ]
  29. }
  30. ]
  31. })

以上这篇浅谈vue 二级路由嵌套和二级路由高亮问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/bingot/article/details/86476646

延伸 · 阅读

精彩推荐