欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue.js Router中嵌套路由的實(shí)用示例

 更新時(shí)間:2021年06月27日 12:20:12   作者:前端小混混  
這篇文章主要給大家介紹了關(guān)于vue.js Router中嵌套路由的相關(guān)資料,所謂嵌套路由就是路由里面嵌套他的子路由,文章通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

隨著 Vue.js 單頁(yè)應(yīng)用(SPA)變得相當(dāng)復(fù)雜,你開(kāi)始需要 Vue 路由以及嵌套路由。嵌套路由允許更復(fù)雜的用戶界面以及相互嵌套的組件。讓我們創(chuàng)建一個(gè)相對(duì)簡(jiǎn)單的用例,來(lái)展示 Vue Router 中嵌套路由的實(shí)用性。

用 Vue CLI 進(jìn)行設(shè)置

如果尚未安裝,請(qǐng)運(yùn)行以下命令全局安裝 Vue CLI:

$ npm install -g @vue/cli

或者

$ yarn global add @vue/cli

現(xiàn)在你能從命令行運(yùn)行 vue 命令了。讓我們創(chuàng)建一個(gè)名為 alligator-nest 的 Vue 應(yīng)用:

$ vue create alligator-nest

在提示符下選擇默認(rèn)預(yù)設(shè)(按 Enter 鍵)。之后,運(yùn)行以下命令:

$ npm install vue-router

然后,在你選擇的編輯器中打開(kāi) alligator-nest 目錄。

基本代碼

以下 CSS 將幫助我們?yōu)?UI 定位元素。將其作為樣式表文件添加到  public/ 文件夾中,并在 public/index.html 中引用它。為此,我們將使用 CSS grid:

grid.css

.row1 {
  grid-row-start: 1;
  grid-row-end: 2;
}

.row12 {
  grid-row-start: 1;
  grid-row-end: 3;
}

.row123 {
  grid-row-start: 1;
  grid-row-end: 4;
}

.row2 {
  grid-row-start: 2;
  grid-row-end: 3;
}

.row23 {
  grid-row-start: 2;
  grid-row-end: 4;
}

.row3 {
  grid-row-start: 3;
  grid-row-end: 4;
}

.col1 {
  grid-column-start: 1;
  grid-column-end: 2;
}

.col12 {
  grid-column-start: 1;
  grid-column-end: 3;
}

.col123 {
  grid-column-start: 1;
  grid-column-end: 4;
}

.col1234 {
  grid-column-start: 1;
  grid-column-end: 5;
}

.col2 {
  grid-column-start: 2;
  grid-column-end: 3;
}

.col23 {
  grid-column-start: 2;
  grid-column-end: 4;
}

.col234 {
  grid-column-start: 2;
  grid-column-end: 5;
}

.col3 {
  grid-column-start: 3;
  grid-column-end: 4;
}

.col34 {
  grid-column-start: 3;
  grid-column-end: 5;
}

.col4 {
  grid-column-start: 4;
  grid-column-end: 5;
}

接下來(lái),讓我們對(duì) vue-cli 添加的默認(rèn)文件進(jìn)行一些更改。

從 src/components 文件夾中刪除 HelloWorld.vue,并從 src/App.vue 中刪除所有與其相關(guān)的東西。對(duì) App.vue 中的 HTML 標(biāo)記和 CSS 樣式進(jìn)行以下修改。

<template>
  <div id="app">
    <h1 class="row1 col12">Alligator Nest</h1>
    <a class="row1 col3">Travels</a>
    <a class="row1 col4">About</a>
    <div class="row2 col234"></div>
  </div>
</template>
html, body {
  height: 100vh;
  width: 100vw;
  padding: 0;
  margin: 0;
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  padding: 2%;
  height: 100%;
  display: grid;
  grid-template-rows: 20% 80%;
  grid-template-columns: 25% 25% 25% 25%;
}

如果你在項(xiàng)目的根目錄中運(yùn)行 npm run serve,則可以將鼠標(biāo)懸停在瀏覽器中的 localhost:8080 上,并查看框架布局。那些 display:grid 屬性很有用!現(xiàn)在我們可以開(kāi)始創(chuàng)建路由了。

輸入 Vue 路由

在 /components 文件夾中創(chuàng)建一個(gè)名為 AboutPage.vue 的組件。它看起來(lái)像這樣:

<template>
  <div>
    <h2>About</h2>
    <p>Alligators were around during the time of the dinosaurs.</p>
  </div>
</template>

<script>
  export default {
    name: 'AboutPage',
  }
</script>

<style scoped>
  
</style>

現(xiàn)在我們的 main.js 文件需要 /about 路由。它看起來(lái)像這樣。

import VueRouter from 'vue-router';
import Vue from 'vue';
import App from './App.vue';

Vue.config.productionTip = false;

import VueRouter from 'vue-router';
Vue.use(VueRouter);

import AboutPage from './components/AboutPage.vue';

const routes = [
  { path: '/about', component: AboutPage },
]

const router = new VueRouter({
  routes
})

new Vue({
  render: h => h(App),
  router
}).$mount('#app');

最后,讓我們回到 App.vue,并將 “About” 的錨標(biāo)記更改為屬性為 to="/about" 的 <router-link> 標(biāo)簽。然后,將第二個(gè) div 更改為 <router-view> 標(biāo)簽。確保保持網(wǎng)格定位類(lèi)屬性不變。

現(xiàn)在,我們有了一個(gè)功能齊全的站點(diǎn)框架,并為 “About” 頁(yè)面處理了路由。

我們?cè)诖酥攸c(diǎn)介紹路由功能,因此不會(huì)在樣式上話費(fèi)太多時(shí)間。盡管如此,我們也要讓Travels 頁(yè)面看起來(lái)更精致一些。

首先,創(chuàng)建一個(gè) TravelPage,方法與創(chuàng)建 AboutPage 相同。在 main.js 中引用它。

還需要?jiǎng)?chuàng)建以下兩個(gè)組件,這些組件最終將嵌套在 TravelPage.vue 中:

TravelAmericaPage.vue

<template>
  <div>
    <p>Alligators can be found in the American states of Louisiana and Florida.</p>
  </div>
</template>

<script>
  export default {
    name: 'TravelAmericaPage'
  }
</script>

<style scoped>
</style>

TravelChinaPage.vue

<template>
  <div>
    <p>Alligators can be found in China's Yangtze River Valley.</p>
  </div>
</template>

<script>
  export default {
    name: 'TravelChinaPage'
  }
</script>

<style scoped>

</style>

配置嵌套路由

現(xiàn)在,讓我們同時(shí)更新 main.js 和 TravelPage.vue,以使用 children 來(lái)引用這些嵌套路由。必須將 main.js 更新為對(duì) routes 常量具有以下定義:

const routes = [
  {
    path: '/travel', component: TravelPage,
    children: [
      { path: '/travel/america', component: TravelAmericaPage },
      { path: '/travel/china', component: TravelChinaPage}
    ]
  },
  {
    path: '/about', component: AboutPage
  }
];

請(qǐng)注意,子級(jí)的嵌套可以無(wú)限繼續(xù)下去。

并且 TravelPage.vue 可以通過(guò)以下方式編寫(xiě):

TravelPage.vue

<template>
  <div id="travel">
    <h2 class="row1">Travels</h2>
    <div class="flex-container row2">
      <router-link to="/travel/america">America</router-link>
      <router-link to="/travel/china">China</router-link>
    </div>
    <router-view class="row3"></router-view>
  </div>
</template>

<script>
  export default {
    name: 'TravelPage'
  }
</script>

<style scoped>
div {
  text-align: center;
}

#travel {
  display: grid;
  grid-template-rows: 20% 40% 40%;
}

.flex-container {
  display: flex;
  justify-content: space-around;
}
</style>

檢出 localhost:8080,你將看到 Travels 頁(yè)面中包含 2 個(gè)子頁(yè)面!當(dāng)你單擊任一鏈接時(shí),我們的 URL 也會(huì)相應(yīng)更新。

總結(jié)

希望本教程對(duì)你了解如何使用嵌套路由有幫助!

關(guān)于該主題的其他注意事項(xiàng)——我們可以使用動(dòng)態(tài)段定義路由,例如 path:'/location/:id'。然后在這些路由的視圖上,可以將該 id 引用為 this.$route.params。當(dāng)你希望在網(wǎng)站和應(yīng)用上顯示更多特定類(lèi)型的數(shù)據(jù)(用戶、圖片等)時(shí),此功能非常有用。

到此這篇關(guān)于vue.js Router中嵌套路由的文章就介紹到這了,更多相關(guān)vue.js嵌套路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue+echarts動(dòng)態(tài)更新數(shù)據(jù)及數(shù)據(jù)變化重新渲染方式

    vue+echarts動(dòng)態(tài)更新數(shù)據(jù)及數(shù)據(jù)變化重新渲染方式

    這篇文章主要介紹了vue+echarts動(dòng)態(tài)更新數(shù)據(jù)及數(shù)據(jù)變化重新渲染方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • vue3.0+vite2實(shí)現(xiàn)動(dòng)態(tài)異步組件懶加載

    vue3.0+vite2實(shí)現(xiàn)動(dòng)態(tài)異步組件懶加載

    學(xué)了vue寫(xiě)項(xiàng)目這么久,忽然發(fā)現(xiàn)路由懶加載的寫(xiě)法,節(jié)省了加載所有路由的時(shí)間。本文主要介紹了vue3.0+vite2實(shí)現(xiàn)動(dòng)態(tài)異步組件懶加載,感興趣的可以了解一下
    2021-06-06
  • Vue實(shí)現(xiàn)過(guò)渡效果的基本方法

    Vue實(shí)現(xiàn)過(guò)渡效果的基本方法

    Vue 提供了一個(gè)強(qiáng)大的過(guò)渡系統(tǒng),可以用于在進(jìn)入、離開(kāi)和列表渲染時(shí)添加各種動(dòng)畫(huà)效果,這些過(guò)渡不僅能夠提升用戶體驗(yàn),還能使界面更加生動(dòng)和吸引人,本文將介紹 Vue 中實(shí)現(xiàn)過(guò)渡效果的基本方法,并提供使用 setup 語(yǔ)法糖的代碼示例,需要的朋友可以參考下
    2024-09-09
  • vue實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能

    vue實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • vue遞歸組件實(shí)戰(zhàn)之簡(jiǎn)單樹(shù)形控件實(shí)例代碼

    vue遞歸組件實(shí)戰(zhàn)之簡(jiǎn)單樹(shù)形控件實(shí)例代碼

    這篇文章主要介紹了vue遞歸組件實(shí)戰(zhàn)之簡(jiǎn)單樹(shù)形控件的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Vue實(shí)現(xiàn)tab切換的兩種方法示例詳解

    Vue實(shí)現(xiàn)tab切換的兩種方法示例詳解

    這篇文章主要介紹了Vue實(shí)現(xiàn)tab切換的兩種方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • Vue頭像處理方案小結(jié)

    Vue頭像處理方案小結(jié)

    這篇文章主要介紹了Vue頭像處理方案,實(shí)現(xiàn)思路主要是通過(guò)獲取后臺(tái)返回頭像url,判斷圖片寬度,高度。具體實(shí)例代碼大家參考下本文
    2018-07-07
  • vue Nprogress進(jìn)度條功能實(shí)現(xiàn)常見(jiàn)問(wèn)題

    vue Nprogress進(jìn)度條功能實(shí)現(xiàn)常見(jiàn)問(wèn)題

    這篇文章主要介紹了vue Nprogress進(jìn)度條功能實(shí)現(xiàn),NProgress是頁(yè)面跳轉(zhuǎn)是出現(xiàn)在瀏覽器頂部的進(jìn)度條,本文通過(guò)實(shí)例代碼給大家講解,需要的朋友可以參考下
    2021-07-07
  • Vue keepAlive頁(yè)面強(qiáng)制刷新方式

    Vue keepAlive頁(yè)面強(qiáng)制刷新方式

    這篇文章主要介紹了Vue keepAlive頁(yè)面強(qiáng)制刷新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)

    el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)

    在開(kāi)發(fā)業(yè)務(wù)場(chǎng)景中我們通常遇到一些奇怪的需求,下面這篇文章主要給大家介紹了關(guān)于el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11

最新評(píng)論