Home > Article > Web Front-end > Detailed explanation of vue router dynamic routing and nested routing examples
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>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 'vue' import Router from 'vue-router' import Hello from '@/components/Hello' import Foo from '@/components/Foo' import Foo2 from '@/components/Foo2' import Foo3 from '@/components/Foo3' Vue.use(Router) export default new Router({ routes: [ {path: '/', redirect: '/hello'}, { path: '/hello', component: Hello, children: [ {path: '/hello/foo', component: Foo}, {path: '/hello/foo2', component: Foo2}, {path: '/hello/foo3', component: Foo3} ] }, { path: '/cc', name: 'Foo', component: Foo } ] })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:
<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: 'app' } </script>Foo.vue
<template> <p> <h1>3434234343</h1> </p> </template> <script> export default { name: 'Foo', 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>Foo2.vue
<template> <p> <h1>this is Foo2</h1> </p> </template> <script> export default { name: 'Foo2', 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>Foo3.vue
<template> <p> <h1>this is foo3</h1> </p> </template> <script> export default { name: 'Foo3', 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>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: 'hello', data () { return { msg: 'Welcome to Your Vue.js App' } } } </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>Routing:
##
import Vue from 'vue' import Router from 'vue-router' import Hello from '@/components/Hello' import Foo from '@/components/Foo' import Foo2 from '@/components/Foo2' import Foo3 from '@/components/Foo3' Vue.use(Router) export default new Router({ routes: [ {path: '/', redirect: '/hello'}, { path: '/hello', component: Hello, children: [ {path: '/hello/foo', component: Foo}, {path: '/hello/foo2', component: Foo2}, {path: '/hello/foo3', component: Foo3} ] }, { path: '/cc', name: 'Foo', component: Foo } ] })
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:
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!