国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue router學(xué)習(xí)之動(dòng)態(tài)路由和嵌套路由詳解

2019-11-19 15:20:30
字體:
供稿:網(wǎng)友

本文主要參考:https://router.vuejs.org/zh-cn/essentials/nested-routes.html

本文的閱讀前提是已經(jīng)能夠搭建一個(gè)vue前臺(tái)程序并且運(yùn)行。如果還么有搭建可以參考文章:
//m.survivalescaperooms.com/article/111650.htm

好,下面上貨。

首先介紹一下動(dòng)態(tài)路由。
動(dòng)態(tài)路由按照我的理解,就是說能夠進(jìn)行頁面的跳轉(zhuǎn),比如說:下面的這個(gè)頁面中:

<template>  <div 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>  </div> </template> 

如果點(diǎn)擊了/hello,那么在router-view中就會(huì)加載對應(yīng)的模塊,也就是在路由中設(shè)置的模塊。

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   }  ] }) 

也就是說,會(huì)跳轉(zhuǎn)到Hello和Foo這兩個(gè)組件。

那么嵌套路由是什么意思呢,最開始我以為的是這樣:/hello/foo 和/hello/foo2這兩個(gè)路由可以簡寫成嵌套路由,其實(shí)不是的。嵌套路由只的是,在子組件中再次嵌套組件。然后在使用路由進(jìn)行跳轉(zhuǎn),這樣跳轉(zhuǎn)的時(shí)候,變化的就只有子組件,而外邊的父組件沒有變化。

下面我把完整的例子放出來,看一下:

App.vue

<template>  <div 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>  </div> </template>  <script> export default {  name: 'app' } </script>  <style> #app {  font-family: 'Avenir', Helvetica, Arial, sans-serif;  -webkit-font-smoothing: antialiased;  -moz-osx-font-smoothing: grayscale;  text-align: center;  color: #2c3e50;  margin-top: 60px; } </style> 

Foo.vue

<template>  <div>   <h1>3434234343</h1>  </div> </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>  <div>   <h1>this is Foo2</h1>  </div> </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>  <div>   <h1>this is foo3</h1>  </div> </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>  <div class="hello">   <h1>{{ msg }}</h1>   <h2>Essential Links</h2>   <ul>    <li><a  rel="external nofollow" target="_blank">Core Docs</a></li>    <li><a  rel="external nofollow" target="_blank">Forum</a></li>    <li><a  rel="external nofollow" target="_blank">Gitter Chat</a></li>    <li><a  rel="external nofollow" target="_blank">Twitter</a></li>    <br>    <li><a  rel="external nofollow" target="_blank">Docs for This Template</a></li>   </ul>   <h2>Ecosystem</h2>   <ul>    <li><a  rel="external nofollow" target="_blank">vue-router</a></li>    <li><a  rel="external nofollow" target="_blank">vuex</a></li>    <li><a  rel="external nofollow" target="_blank">vue-loader</a></li>    <li><a  rel="external nofollow" target="_blank">awesome-vue</a></li>   </ul>   <div>    <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>   </div>   <router-view style="border: solid 1px blue"></router-view>  </div> </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> 

路由:

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   }  ] }) 

需要注意的是仔細(xì)的看App.vue和Hello.vue中,都包含<router-view></router-view>,但是他們的作用不同,App.vue是頂層路由,指的是組外層的路由,Hello.vue中的是嵌套路由,負(fù)責(zé)顯示子組件。
我把頁面截圖一下:

 

這個(gè)界面,點(diǎn)擊最上邊的 / 或者/hello 或者/cc的時(shí)候,發(fā)生變化的是紅色路由中的內(nèi)容。當(dāng)點(diǎn)擊/hello/foo /hello/foo2 /hello/foo3 的時(shí)候,發(fā)生變化的是下面藍(lán)色路由中的內(nèi)容。

這樣就和我們平時(shí)應(yīng)用十分的相似了。最外層于有變化,或者局部有變化,但是不想全局的發(fā)生改變。

同時(shí),這樣也符合了模塊化,各個(gè)模塊分別在不同的模塊中。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海阳市| 永胜县| 汉沽区| 蚌埠市| 玛多县| 济宁市| 托克托县| 阿勒泰市| 牡丹江市| 泰州市| 博白县| 卢湾区| 西盟| 德惠市| 介休市| 中牟县| 竹溪县| 阜城县| 凯里市| 修武县| 庆元县| 柳河县| 西吉县| 利川市| 交口县| 江都市| 静乐县| 青河县| 扎兰屯市| 陕西省| 呼和浩特市| 延安市| 南通市| 榆中县| 上饶县| 巴林左旗| 彭水| 乌鲁木齐县| 富宁县| 三台县| 柯坪县|