vue.js Router中嵌套路由的實用示例
前言
隨著 Vue.js 單頁應(yīng)用(SPA)變得相當(dāng)復(fù)雜,你開始需要 Vue 路由以及嵌套路由。嵌套路由允許更復(fù)雜的用戶界面以及相互嵌套的組件。讓我們創(chuàng)建一個相對簡單的用例,來展示 Vue Router 中嵌套路由的實用性。
用 Vue CLI 進(jìn)行設(shè)置
如果尚未安裝,請運行以下命令全局安裝 Vue CLI:
$ npm install -g @vue/cli
或者
$ yarn global add @vue/cli
現(xiàn)在你能從命令行運行 vue 命令了。讓我們創(chuàng)建一個名為 alligator-nest 的 Vue 應(yīng)用:
$ vue create alligator-nest
在提示符下選擇默認(rèn)預(yù)設(shè)(按 Enter 鍵)。之后,運行以下命令:
$ npm install vue-router
然后,在你選擇的編輯器中打開 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;
}
接下來,讓我們對 vue-cli 添加的默認(rèn)文件進(jìn)行一些更改。
從 src/components 文件夾中刪除 HelloWorld.vue,并從 src/App.vue 中刪除所有與其相關(guān)的東西。對 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%;
}
如果你在項目的根目錄中運行 npm run serve,則可以將鼠標(biāo)懸停在瀏覽器中的 localhost:8080 上,并查看框架布局。那些 display:grid 屬性很有用!現(xiàn)在我們可以開始創(chuàng)建路由了。
輸入 Vue 路由
在 /components 文件夾中創(chuàng)建一個名為 AboutPage.vue 的組件。它看起來像這樣:
<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 路由。它看起來像這樣。
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)簽。然后,將第二個 div 更改為 <router-view> 標(biāo)簽。確保保持網(wǎng)格定位類屬性不變。
現(xiàn)在,我們有了一個功能齊全的站點框架,并為 “About” 頁面處理了路由。
我們在此重點介紹路由功能,因此不會在樣式上話費太多時間。盡管如此,我們也要讓Travels 頁面看起來更精致一些。
首先,創(chuàng)建一個 TravelPage,方法與創(chuàng)建 AboutPage 相同。在 main.js 中引用它。
還需要創(chuàng)建以下兩個組件,這些組件最終將嵌套在 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)在,讓我們同時更新 main.js 和 TravelPage.vue,以使用 children 來引用這些嵌套路由。必須將 main.js 更新為對 routes 常量具有以下定義:
const routes = [
{
path: '/travel', component: TravelPage,
children: [
{ path: '/travel/america', component: TravelAmericaPage },
{ path: '/travel/china', component: TravelChinaPage}
]
},
{
path: '/about', component: AboutPage
}
];
請注意,子級的嵌套可以無限繼續(xù)下去。
并且 TravelPage.vue 可以通過以下方式編寫:
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 頁面中包含 2 個子頁面!當(dāng)你單擊任一鏈接時,我們的 URL 也會相應(yīng)更新。
總結(jié)
希望本教程對你了解如何使用嵌套路由有幫助!
關(guān)于該主題的其他注意事項——我們可以使用動態(tài)段定義路由,例如 path:'/location/:id'。然后在這些路由的視圖上,可以將該 id 引用為 this.$route.params。當(dāng)你希望在網(wǎng)站和應(yīng)用上顯示更多特定類型的數(shù)據(jù)(用戶、圖片等)時,此功能非常有用。
到此這篇關(guān)于vue.js Router中嵌套路由的文章就介紹到這了,更多相關(guān)vue.js嵌套路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js中關(guān)于偵聽器(watch)的高級用法示例
Vue.js 提供了一個方法 watch,它用于觀察Vue實例上的數(shù)據(jù)變動。下面這篇文章主要給大家介紹了關(guān)于Vue.js中關(guān)于偵聽器(watch)的高級用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05
vue中g(shù)etters的使用與四個map方法的使用方式
這篇文章主要介紹了vue中g(shù)etters的使用與四個map方法的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
說說如何使用Vuex進(jìn)行狀態(tài)管理(小結(jié))
這篇文章主要介紹了說說如何使用Vuex進(jìn)行狀態(tài)管理(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
基于Vue實現(xiàn)關(guān)鍵詞實時搜索高亮顯示關(guān)鍵詞
這篇文章主要介紹了基于Vue實現(xiàn)關(guān)鍵詞實時搜索高亮顯示關(guān)鍵詞,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

