12.Vue嵌套路由(三层)

Vue嵌套路由:
实现效果(路由三层嵌套,点击一级tab显示二级tab效果,二级tab点击切换对应内容,不在tab区域里的内容,切换时不重复渲染):

Demo访问时路径:http://IP:端口/#/routers/

image.png

1.建立案例文件夹 page/routers/

image.png

1 routers/index.vue

<template>
  <div>
      <router-link :to="{name: 'rindex_rhome'}" class="rlink" :class="{active:selected == 1}" @click.native="tabck(1)">首页</router-link>
      <router-link :to="{name: 'rindex_rnews'}" class="rlink" class="{active:selected == 2}" @click.native="tabck(2)">新闻</router-link>
      <router-link :to="{name: 'rindex_ryl'}" class="rlink" class="{active:selected == 3}" @click.native="tabck(3)">娱乐</router-link>
    
       <!-- 二级子路由页面 -->
        <router-view />
  </div>
</template>
<script>
export default {
  data(){
    return {
          selected: 1
    }
  },
  methods: {
    tabck(index){
        this.selected = index; //设置tab选中项
    }
  }
}
</script>
<style>
  .rlink {
    padding: 5px;
    margin: 5px;
    margin-bottom: 10px;
    display: inline-block;
    text-decoration: none;
    color: blue;
  }

  .rlink.active {
    color: red;
    text-decoration: underline;
  }
</style>

1-1-1 routers/home/index.vue

<template>
  <div>
      HOME页面信息:<br/>
      <router-link :to="{name: 'rindex_rhome_Rhomezx'}" class="rlink" :class="{active:selected == 1}" @click.native="tabck(1)">最新HOME</router-link>
      <router-link :to="{name: 'rindex_rhome_Rhomegj'}" class="rlink" :class="{active:selected == 2}" @click.native="tabck(2)">国际HOME</router-link>
      <router-link :to="{name: 'rindex_rhome_Rhomegn'}" class="rlink" :class="{active:selected == 3}" @click.native="tabck(3)">国内HOME</router-link>
    
    <!-- 子路由(三层) -->
      <router-view />
  </div>
</template>
<script>
  export default {
    data(){
        return {
            selected: 1
        }
    },
    methods: {
      tabck(index) {
         this.selected = index; //设置选中tab
      }
    }
  }
</script>
<style>
</style>

1-1-2 routers/home/tab/gj.vue、gn.vue、zx.vue

gj.vue:

<template>
  <div>
        国际HOME信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'国际HOME'},
            {name:'国际HOME'},
            {name:'国际HOME'}
          ]
      }
    }
  }
</script>


gn.vue :

<template>
  <div>
        国内HOME信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'国内HOME'},
            {name:'国内HOME'}
          ]
      }
    }
  }
</script>

zx.vue:

<template>
  <div>
        最新HOME信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'最新HOME'}
          ]
      }
    }
  }
</script>

1-2 routers/news/index.vue

<template>
  <div>
       新闻页面信息:<br/>
        <router-link class="rlink" :class="{active:selected == 1}" @cllick.native="tabck(1)">最新新闻</router-link>
        <router-link class="rlink" :class="{active:selected == 2}" @cllick.native="tabck(2)">国际新闻</router-link>
        <router-link class="rlink" :class="{active:selected == 3}" @cllick.native="tabck(3)">国内新闻</router-link>
    <!-- 子路由 -->
    <router-view/>
  </div>
</template>
<script>
  export default {
    data () {
      return {
          selected: 1
      }
    },
    methods: {
      tabck(index){
        this.selected = index; //切换tab,设置选中项
      }
    } 
  }
</script>

1-2-1 routers/news/tab/gj.vue、gn.vue、zx.vue

gj.vue:

<template>
  <div>
        国际新闻信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'国际新闻信息'},
            {name:'国际新闻信息'},
            {name:'国际新闻信息'},
            {name:'国际新闻信息'},
            {name:'国际新闻信息'}
          ]
      }
    }
  }
</script>

gn.vue:

<template>
  <div>
        国内新闻信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'国内新闻信息'},
            {name:'国内新闻信息'}
          ]
      }
    }
  }
</script>

zx.vue:

<template>
  <div>
        最新新闻信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'最新新闻信息'},
            {name:'最新新闻信息'}
          ]
      }
    }
  }
</script>

1-3-1 routers/yl/index.vue

<template>
  <div>
        娱乐页面信息:<br/>
        <router-link class="rlink" :class="{active:selected == 1}" @click.native="tabck(1)">最新娱乐</router-link>
        <router-link class="rlink" :class="{active:selected == 2}" @click.native="tabck(2)">明星娱乐</router-link>
        <router-link class="rlink" :class="{active:selected == 3}" @click.native="tabck(3)">焦点娱乐</router-link>
      
        <!-- 子路由-->
        <router-view/>
  </div>
  <script>
    export default {
      data(){
        return {
          selected: 1
        }
      },
      methods: {
        tabck(index){
          this.selected = index; //设置tab选中项
        }
      }
    }
  </script>
</template>

1-3-2 routers/yl/tab/jd.vue、mx.vue、zx.vue

jd.vue:
<template>
  <div>
        焦点娱乐信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'焦点娱乐信息'},
            {name:'焦点娱乐信息'}
          ]
      }
    }
  }
</script>

mx.vue:

<template>
  <div>
        明星娱乐信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'明星娱乐信息'},
            {name:'明星娱乐信息'}
          ]
      }
    }
  }
</script>

zx.vue:

<template>
  <div>
        最新娱乐信息:<br/>
         <ul >
             <li v-for="(result, index) in list" :key="index">{{result.name}}{{index}}</li>
         </ul>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          list: [
            {name:'最新娱乐信息'},
            {name:'最新娱乐信息'}
          ]
      }
    }
  }
</script>

2.路由配置规则(router/index.js)

....
  省略导入路由、使用路由代码...
....
// 嵌套路由的使用:第一层
import Rindex from '../page/routers/index'
// 嵌套路由的使用:第二层
import Rhome from '../page/routers/home/index'
// 嵌套路由的使用:第三层
import Rhomezx from '../page/routers/home/tab/zx'
import Rhomegj from '../page/routers/home/tab/gj'
import Rhomegn from '../page/routers/home/tab/gn'

import Rnews from '../page/routers/news/index'
import Rnewszx from '../page/routers/news/tab/zx'
import Rnewsgj from '../page/routers/news/tab/gj'
import Rnewsgn from '../page/routers/news/tab/gn'

import Ryl from '../page/routers/yl/index'
import Rylzx from '../page/routers/yl/tab/zx'
import Rylmx from '../page/routers/yl/tab/mx'
import Ryljd from '../page/routers/yl/tab/jd'

// 路由规则配置:
export default new Router({
  routes : [
    {
      name: 'rindex',
      path: '/routers',
      component: Rindex,
      redirect: {name: 'rindex_rhome'}, // 跳转到下一级第一个
      children: [
         {
             name: 'rindex_rhome',
             path: 'rindex_rhome', //如果这里不使用 "/rhome" 则表示是归属于上级路由(上级luyou/子path),如果使用 "/rhome" 则表示根路径下访问
             component: Rhome,
             redirect: {name: 'rindex_rhome_Rhomezx'}, //跳转到下级第一层
             children: [
                {
                  name: 'rindex_rhome_Rhomezx',
                  path: 'rindex_rhome_Rhomezx',
                  component: Rhomezx
                },
                {
                  name: 'rindex_rhome_Rhomegj',
                  path: 'rindex_rhome_Rhomegj',
                  component: Rhomegj
                },
                {
                  name: 'rindex_rhome_Rhomegn',
                  path: 'rindex_rhome_Rhomegn',
                  component: Rhomegn
                }
            ]
         },
         {
            name: 'rindex_rnews',
            path: 'rindex_rnews',
            component: Rnews,
            redirect: {name: 'rindex_rnews_Rnewszx'},
            children: [
              {
                  name: 'rindex_rnews_Rnewszx',
                  path: 'rindex_rnews_Rnewszx',
                  component: Rnewszx
              },
              {
                  name: 'rindex_rnews_Rnewsgj',
                  path: 'rindex_rnews_Rnewsgj',
                  component: Rnewsgj
              },
              {
                  name: 'rindex_rnews_Rnewsgn',
                  path: 'rindex_rnews_Rnewsgn',
                  component: Rnewsgn
              }
            ]
        },
        {
            name: 'rindex_ryl',
            path: 'rindex_ryl',
            component: Ryl,
            redirect: {name: 'rindex_ryl_rylzx'},
            children:[
                {
                    name: 'rindex_ryl_rylzx',
                    path: 'rindex_ryl_rylzx',
                    component: Rylzx
                },
                {
                    name: 'rindex_ryl_rylmx',
                    path: 'rindex_ryl_rylmx',
                    component: Rylmx
                },
                {
                    name: 'rindex_ryl_ryljd',
                    path: 'rindex_ryl_ryljd',
                    component: Ryljd
                }
            ]
        }
      ]
    }
  ]
});

推荐阅读更多精彩内容

  • 转载 :OpenDiggawesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库...
    果汁密码阅读 22,663评论 8 125
  • 目录 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element★13489 ...
    余生社会阅读 18,935评论 7 233
  • 今天真算的上是我工作来,印象最深刻的一天了。✊ 因为明天开始连续三天外出学习,所以尽快的跟其他科目的老师调课,...
    姜雪萍阅读 404评论 9 8
  • 我的笔记小结 经济学到底是否建立在“人是理性的”基础之上? 我们知道,人不是永远都处在理性的状态当中的,也就是说,...
    LemonHohO阅读 420评论 0 1
  • 这部电影对奥黛丽•赫本影响之大,难于言说,也许是对她而言,最重要的一部作品吧。因为赫本和路加修女是合二为一的,她们...
    霍甜阅读 500评论 0 3