溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何實現Nuxt內導航欄

發布時間:2020-08-03 11:36:00 來源:億速云 閱讀:319 作者:小豬 欄目:web開發

這篇文章主要講解了如何實現Nuxt內導航欄,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

方式一 | 通過嵌套路由實現

在pages頁面根據nuxt的路由規則,建立頁面

1. 創建文件目錄及文件

如何實現Nuxt內導航欄

根據規則,如果要創建子路由,子路由的文件夾名字,必須和父路由名字相同

所以,我們的文件夾也為index,index文件夾需要一個默認的頁面不然nuxt的路由規則就不能正確匹配頁面

一級路由是根路由

二級路由是index,user,默認進入index路由

下面是router頁面自動生成的路由

{
  path: "/",
  component: _93624e48,
  children: [{
   path: "",
   component: _7ba30c26,
   name: "index"
  }, {
   path: "user",
   component: _6934afa7,
   name: "index-user"
  }]
 }

2. html頁面增加nutx-child配合子路由跳轉

<template>
 <div class="container">
  <div>
   <logo />
   <h2 class="title">
    nuxt-demo
   </h2>
   // 直接訪問路由
   <!-- <nuxt-link to="/users">用戶列表</nuxt-link> -->
   // 通過push的方式直接訪問路由路徑
   <!-- <el-button @click="$router.push('/users')">用戶列表</el-button> -->
   // 通過push的方式,同時用對象的方式訪問路由
   <el-button @click="$router.push({name: 'index'})">首頁</el-button>
   <el-button @click="$router.push({name: 'index-user'})">用戶詳情</el-button>
  </div>
  // nuxt規定的子路由插槽
  <nuxt-child></nuxt-child>
 </div>
</template>

這里就拿官方demo改了一下,可以看到,切換路由的時候,只有子路由頁面是變換的,父路由部分是沒有變換的

如何實現Nuxt內導航欄 

方式二 | 創建公共組件實現

這個方法是需要用到vuex的,當然了,如果嫌麻煩,用storage也行

在components內創建公共組件

1.在pages文件夾創建頁面,一個主頁,一個用戶頁面,一個活動頁面

如何實現Nuxt內導航欄

創建頁面的過程就不一一細說了,具體就是文件夾下面一個index.vue,router就會讀這個index為路由指定的頁面

我們看下.nuxt文件夾下面的router.js頁面

如何實現Nuxt內導航欄 

這就是建立好的路由

2. 創建公共組件

如何實現Nuxt內導航欄 

這里偷個懶,用的element的導航欄組件

<template>
 <div id="nav-wrapper">
  <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
   <el-menu-item index="1" @click="$router.push({name: 'index'})">首頁</el-menu-item>
   <el-menu-item index="3" @click="$router.push({name: 'users'})">用戶頁面</el-menu-item>
   <el-menu-item index="4" @click="$router.push({name: 'active'})">活動頁面</el-menu-item>
  </el-menu>
 </div>
</template>

3. 在所有路由頁面導入創建的公共組件

<template>
 <div class="container">
  <div>
   <logo />
   <h2 class="title">
    nuxt-demo
   </h2>
   <navBar />
  </div>
 </div>
</template>

<script>
import Logo from '~/components/Logo.vue'
import navBar from '~/components/nav.vue'

export default {
 components: {
  Logo,
  navBar
 }
}
</script>

<style>

這樣就完成了第一步,我們看下預覽

如何實現Nuxt內導航欄

問題出現了,雖然我們的路由變換了,但是導航欄的狀態確沒有同步,因為路由跳轉的時候,組件狀態會刷新,所以這個時候,需要共享狀態,所以,我這里用的是vuex

4. 使用vuex同步導航欄狀態

直接在store文件夾內進行添加就行,nuxt里推薦的兩種vuex使用方法

第一種是普通創建

如何實現Nuxt內導航欄

第二種是模塊化創建

如何實現Nuxt內導航欄

這里我選的是第二種方式,我也建議使用這種,因為方便維護,各種狀態一目了然

我們看下目錄結構,這里和在vue使用的vuex目錄是一樣的

如何實現Nuxt內導航欄

這里就不一一詳細說明每個文件內容了,本次重點是使用vuex來同步狀態

我們把狀態同步到vuex中,這樣每次頁面進來的時候,直接讀取vuex中的數據,就可以同步導航欄狀態欄了

4.1 vuex使用報錯

store/index.js should export a method that returns a Vuex

instance.vuex在nuxt中是需要導出一個store實例

我們這里需要改動一下store文件下的index頁面

如何實現Nuxt內導航欄 

我們繼續回到導航欄組件內

<template>
 <div id="nav-wrapper">
  <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
   <el-menu-item index="1" @click="$router.push({name: 'index'})">首頁</el-menu-item>
   <el-menu-item index="3" @click="$router.push({name: 'users'})">用戶頁面</el-menu-item>
   <el-menu-item index="4" @click="$router.push({name: 'active'})">活動頁面</el-menu-item>
  </el-menu>
 </div>
</template>

<script>
 import {mapGetters, mapMutations} from 'vuex'
 export default{
  data() {
   return {
    activeIndex: '1',
    activeIndex2: '1'
   };
  },
  computed: {
   ...mapGetters([
    'barIndex'
   ])
  },
  methods: {
   ...mapMutations({
    'change_index': 'CHANGE_INDEX'
   }),
   handleSelect(key, keyPath) {
    console.log(key, keyPath);
    this.activeIndex = key
    // 每次切換導航欄,會把當前狀態同步到vuex中
    this.change_index(this.activeIndex)
   }
  },
  created() {
   if (this.barIndex) { // 判斷vuex內是否有上一次存儲的數據,有就同步到當前狀態
    this.activeIndex = this.barIndex
   }
   console.log('vuex', this.activeIndex)
  }
 }
</script>

這樣,我們就已經可以同步導航欄狀態了

如何實現Nuxt內導航欄

看完上述內容,是不是對如何實現Nuxt內導航欄有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女