在 Vue.js 中使用嵌套路由
隨著 Vue.js 單頁應(yīng)用(SPA)變得相當(dāng)復(fù)雜,你開始需要 Vue 路由以及嵌套路由。嵌套路由允許更復(fù)雜的用戶界面以及相互嵌套的組件。讓我們創(chuàng)建一個(gè)相對(duì)簡(jiǎn)單的用例,來展示 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
然后,在你選擇的編輯器中打開 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;
- }
接下來,讓我們對(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)在我們可以開始創(chuàng)建路由了。
輸入 Vue 路由
在 /components 文件夾中創(chuàng)建一個(gè)名為 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"的
現(xiàn)在,我們有了一個(gè)功能齊全的站點(diǎn)框架,并為 “About” 頁面處理了路由。
我們?cè)诖酥攸c(diǎn)介紹路由功能,因此不會(huì)在樣式上話費(fèi)太多時(shí)間。盡管如此,我們也要讓Travels 頁面看起來更精致一些。
首先,創(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 來引用這些嵌套路由。必須將 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í)的嵌套可以無限繼續(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 個(gè)子頁面!當(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)用上顯示更多特定類型的數(shù)據(jù)(用戶、圖片等)時(shí),此功能非常有用。