溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue嵌套路由怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2022-11-10 09:19:33 來源:億速云 閱讀:106 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“vue嵌套路由怎么實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“vue嵌套路由怎么實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(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">
    <h2 class="row1 col12">Alligator Nest</h2>
    <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>
    <h3>About</h3>
    <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)簽。然后,將第二個(gè) div 更改為 <router-view> 標(biāo)簽。確保保持網(wǎng)格定位類屬性不變。

現(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">
    <h3 class="row1">Travels</h3>
    <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)更新。

vue是什么

Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫只關(guān)注視圖層,方便與第三方庫和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復(fù)雜的單頁應(yīng)用。

讀到這里,這篇“vue嵌套路由怎么實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI