Home > Web Front-end > JS Tutorial > body text

Detailed explanation of vue router dynamic routing and nested routing examples

小云云
Release: 2018-02-03 14:45:32
Original
3259 people have browsed it

This article mainly introduces vue router dynamic routing and nested routing to you. It introduces the use of dynamic routing and nested routing in detail. If you are interested, you can learn more. I hope it can help you.

First introduce dynamic routing.
According to my understanding, dynamic routing means that it can jump to the page. For example: in the following page:


##

<template> 
 <p id="app"> 
  <header> 
   <router-link to="/">/</router-link> 
   <router-link to="/hello">/hello</router-link> 
   <router-link to="/cc">/cc</router-link> 
  </header> 
  <router-view style="border: 1px solid red"></router-view> 
 </p> 
</template>
Copy after login

If you click /hello, Then the corresponding module will be loaded in router-view, which is the module set in the routing.


import Vue from &#39;vue&#39; 
import Router from &#39;vue-router&#39; 
import Hello from &#39;@/components/Hello&#39; 
import Foo from &#39;@/components/Foo&#39; 
import Foo2 from &#39;@/components/Foo2&#39; 
import Foo3 from &#39;@/components/Foo3&#39; 
 
Vue.use(Router) 
 
export default new Router({ 
 routes: [ 
  {path: &#39;/&#39;, redirect: &#39;/hello&#39;}, 
  { 
   path: &#39;/hello&#39;, 
   component: Hello, 
   children: [ 
    {path: &#39;/hello/foo&#39;, component: Foo}, 
    {path: &#39;/hello/foo2&#39;, component: Foo2}, 
    {path: &#39;/hello/foo3&#39;, component: Foo3} 
   ] 
  }, 
  { 
   path: &#39;/cc&#39;, 
   name: &#39;Foo&#39;, 
   component: Foo 
  } 
 ] 
})
Copy after login
Copy after login

In other words, it will jump to the two components Hello and Foo.

So what does nested routing mean? At first I thought it was this: the two routes /hello/foo and /hello/foo2 can be abbreviated as nested routing, but in fact they are not. Nested routing only nests components again within child components. Then use routing to jump, so that when jumping, only the child components change, and the outer parent component does not change.

I will post the complete example below, take a look:


App.vue


<template> 
 <p id="app"> 
  <header> 
   <router-link to="/">/</router-link> 
   <router-link to="/hello">/hello</router-link> 
   <router-link to="/cc">/cc</router-link> 
  </header> 
  <router-view style="border: 1px solid red"></router-view> 
 </p> 
</template> 
 
<script> 
export default { 
 name: &#39;app&#39; 
} 
</script> 
 
Copy after login

Foo.vue


<template> 
 <p> 
  <h1>3434234343</h1> 
 </p> 
</template> 
 
<script> 
 export default { 
  name: &#39;Foo&#39;, 
  data () { 
   return { 
   } 
  } 
 } 
</script> 
 
<!-- Add "scoped" attribute to limit CSS to this component only --> 
<style scoped> 
 h1, h2 { 
  font-weight: normal; 
 } 
 
 ul { 
  list-style-type: none; 
  padding: 0; 
 } 
 
 li { 
  display: inline-block; 
  margin: 0 10px; 
 } 
 
 a { 
  color: #42b983; 
 } 
</style>
Copy after login

Foo2.vue


<template> 
 <p> 
  <h1>this is Foo2</h1> 
 </p> 
</template> 
 
<script> 
 export default { 
  name: &#39;Foo2&#39;, 
  data () { 
   return { 
   } 
  } 
 } 
</script> 
 
<!-- Add "scoped" attribute to limit CSS to this component only --> 
<style scoped> 
 h1, h2 { 
  font-weight: normal; 
 } 
 
 ul { 
  list-style-type: none; 
  padding: 0; 
 } 
 
 li { 
  display: inline-block; 
  margin: 0 10px; 
 } 
 
 a { 
  color: #42b983; 
 } 
</style>
Copy after login

Foo3.vue


<template> 
 <p> 
  <h1>this is foo3</h1> 
 </p> 
</template> 
 
<script> 
 export default { 
  name: &#39;Foo3&#39;, 
  data () { 
   return { 
   } 
  } 
 } 
</script> 
 
<!-- Add "scoped" attribute to limit CSS to this component only --> 
<style scoped> 
 h1, h2 { 
  font-weight: normal; 
 } 
 
 ul { 
  list-style-type: none; 
  padding: 0; 
 } 
 
 li { 
  display: inline-block; 
  margin: 0 10px; 
 } 
 
 a { 
  color: #42b983; 
 } 
</style>
Copy after login

Hello.vue


<template> 
 <p class="hello"> 
  <h1>{{ msg }}</h1> 
  <h2>Essential Links</h2> 
  <ul> 
   <li><a href="https://vuejs.org" rel="external nofollow" target="_blank">Core Docs</a></li> 
   <li><a href="https://forum.vuejs.org" rel="external nofollow" target="_blank">Forum</a></li> 
   <li><a href="https://gitter.im/vuejs/vue" rel="external nofollow" target="_blank">Gitter Chat</a></li> 
   <li><a href="https://twitter.com/vuejs" rel="external nofollow" target="_blank">Twitter</a></li> 
   <br> 
   <li><a href="http://vuejs-templates.github.io/webpack/" rel="external nofollow" target="_blank">Docs for This Template</a></li> 
  </ul> 
  <h2>Ecosystem</h2> 
  <ul> 
   <li><a href="http://router.vuejs.org/" rel="external nofollow" target="_blank">vue-router</a></li> 
   <li><a href="http://vuex.vuejs.org/" rel="external nofollow" target="_blank">vuex</a></li> 
   <li><a href="http://vue-loader.vuejs.org/" rel="external nofollow" target="_blank">vue-loader</a></li> 
   <li><a href="https://github.com/vuejs/awesome-vue" rel="external nofollow" target="_blank">awesome-vue</a></li> 
  </ul> 
  <p> 
   <router-link to="/hello/foo">/hello/foo</router-link> 
   <router-link to="/hello/foo2">/hello/foo2</router-link> 
   <router-link to="/hello/foo3">/hello/foo3</router-link> 
  </p> 
  <router-view style="border: solid 1px blue"></router-view> 
 </p> 
</template> 
<script> 
 export default { 
  name: &#39;hello&#39;, 
  data () { 
   return { 
    msg: &#39;Welcome to Your Vue.js App&#39; 
   } 
  } 
 } 
</script> 
 
<!-- Add "scoped" attribute to limit CSS to this component only --> 
<style scoped> 
 h1, h2 { 
  font-weight: normal; 
 } 
 
 ul { 
  list-style-type: none; 
  padding: 0; 
 } 
 
 li { 
  display: inline-block; 
  margin: 0 10px; 
 } 
 
 a { 
  color: #42b983; 
 } 
</style>
Copy after login

Routing:

##

import Vue from &#39;vue&#39; 
import Router from &#39;vue-router&#39; 
import Hello from &#39;@/components/Hello&#39; 
import Foo from &#39;@/components/Foo&#39; 
import Foo2 from &#39;@/components/Foo2&#39; 
import Foo3 from &#39;@/components/Foo3&#39; 
 
Vue.use(Router) 
 
export default new Router({ 
 routes: [ 
  {path: &#39;/&#39;, redirect: &#39;/hello&#39;}, 
  { 
   path: &#39;/hello&#39;, 
   component: Hello, 
   children: [ 
    {path: &#39;/hello/foo&#39;, component: Foo}, 
    {path: &#39;/hello/foo2&#39;, component: Foo2}, 
    {path: &#39;/hello/foo3&#39;, component: Foo3} 
   ] 
  }, 
  { 
   path: &#39;/cc&#39;, 
   name: &#39;Foo&#39;, 
   component: Foo 
  } 
 ] 
})
Copy after login
Copy after login

It should be noted that you should look carefully at App.vue and Hello.vue, both contain , but their functions are different. App.vue is the top-level route, which refers to the route outside the group. In Hello.vue, it is Nested routing, responsible for displaying child components.

I will take a screenshot of the page:


In this interface, when you click / or /hello or /cc at the top, what changes is the content in the red route . When clicking /hello/foo /hello/foo2 /hello/foo3, what changes is the content in the blue route below.


This is very similar to our daily application. There are changes in the outermost layer, or there are changes locally, but we don't want the global changes to occur.


At the same time, this also conforms to modularity, and each module is in a different module.


Related recommendations:


nginx_lua case analysis: dynamic routing implementation

The above is the detailed content of Detailed explanation of vue router dynamic routing and nested routing examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template