search
HomeWeb Front-endJS TutorialHow to use Vue three-layer nested routing

This time I will show you how to use Vue three-layer nested routing, and what are the precautions when using Vue three-layer nested routing. The following is a practical case, let's take a look.

Vue nested routing:

Achieve the effect (the routing is nested at three levels, click on the first-level tab to display the second-level tab effect, click on the second-level tab to switch the corresponding content, Content not in the tab area will not be rendered repeatedly when switching):

Demo access path: http://IP:port/#/routers/

1. Create the case folder page/routers/

1 routers/index.vue

<template>
 <p>
   <router-link>首页</router-link>
   <router-link>新闻</router-link>
   <router-link>娱乐</router-link>
  
    <!-- 二级子路由页面 -->
    <router-view></router-view>
 </p>
</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>
 <p>
   HOME页面信息:<br>
   <router-link>最新HOME</router-link>
   <router-link>国际HOME</router-link>
   <router-link>国内HOME</router-link>
  
  <!-- 子路由(三层) -->
   <router-view></router-view>
 </p>
</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>
 <p>
    国际HOME信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;国际HOME&#39;},
      {name:&#39;国际HOME&#39;},
      {name:&#39;国际HOME&#39;}
     ]
   }
  }
 }
</script>

gn.vue:

<template>
 <p>
    国内HOME信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;国内HOME&#39;},
      {name:&#39;国内HOME&#39;}
     ]
   }
  }
 }
</script>

zx.vue:

<template>
 <p>
    最新HOME信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;最新HOME&#39;}
     ]
   }
  }
 }
</script>

1-2 routers/news/index.vue

<template>
 <p>
    新闻页面信息:<br>
    <router-link>最新新闻</router-link>
    <router-link>国际新闻</router-link>
    <router-link>国内新闻</router-link>
  <!-- 子路由 -->
  <router-view></router-view>
 </p>
</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>
 <p>
    国际新闻信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;国际新闻信息&#39;},
      {name:&#39;国际新闻信息&#39;},
      {name:&#39;国际新闻信息&#39;},
      {name:&#39;国际新闻信息&#39;},
      {name:&#39;国际新闻信息&#39;}
     ]
   }
  }
 }
</script>

gn.vue:

<template>
 <p>
    国内新闻信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;国内新闻信息&#39;},
      {name:&#39;国内新闻信息&#39;}
     ]
   }
  }
 }
</script>

zx.vue:

<template>
 <p>
    最新新闻信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;最新新闻信息&#39;},
      {name:&#39;最新新闻信息&#39;}
     ]
   }
  }
 }
</script>

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

<template>
 <p>
    娱乐页面信息:<br>
    <router-link>最新娱乐</router-link>
    <router-link>明星娱乐</router-link>
    <router-link>焦点娱乐</router-link>
   
    <!-- 子路由-->
    <router-view></router-view>
 </p>
 <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>
 <p>
    焦点娱乐信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;焦点娱乐信息&#39;},
      {name:&#39;焦点娱乐信息&#39;}
     ]
   }
  }
 }
</script>

mx.vue:

<template>
 <p>
    明星娱乐信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;明星娱乐信息&#39;},
      {name:&#39;明星娱乐信息&#39;}
     ]
   }
  }
 }
</script>

zx.vue:

<template>
 <p>
    最新娱乐信息:<br>
     </p>
<ul>
       <li>{{result.name}}{{index}}</li>
     </ul>
 
</template>
<script>
 export default {
  data(){
   return {
     list: [
      {name:&#39;最新娱乐信息&#39;},
      {name:&#39;最新娱乐信息&#39;}
     ]
   }
  }
 }
</script>

2. Routing configuration rules (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'},
      chidren:[
        {
          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
        }
      ]
    }
   ]
  }
 ]
});

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Summary of how to use data-* attributes in H5

How to load jquery in a JS file. js

The above is the detailed content of How to use Vue three-layer nested routing. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java vs JavaScript: A Detailed Comparison for DevelopersJava vs JavaScript: A Detailed Comparison for DevelopersMay 16, 2025 am 12:01 AM

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.