詳解Nuxt內(nèi)導(dǎo)航欄的兩種實現(xiàn)方式
方式一 | 通過嵌套路由實現(xiàn)
在pages頁面根據(jù)nuxt的路由規(guī)則,建立頁面
1. 創(chuàng)建文件目錄及文件

根據(jù)規(guī)則,如果要創(chuàng)建子路由,子路由的文件夾名字,必須和父路由名字相同
所以,我們的文件夾也為index,index文件夾需要一個默認的頁面不然nuxt的路由規(guī)則就不能正確匹配頁面
一級路由是根路由
二級路由是index,user,默認進入index路由
下面是router頁面自動生成的路由
{
path: "/",
component: _93624e48,
children: [{
path: "",
component: _7ba30c26,
name: "index"
}, {
path: "user",
component: _6934afa7,
name: "index-user"
}]
}
2. html頁面增加nutx-child配合子路由跳轉(zhuǎn)
<template>
<div class="container">
<div>
<logo />
<h1 class="title">
nuxt-demo
</h1>
// 直接訪問路由
<!-- <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規(guī)定的子路由插槽
<nuxt-child></nuxt-child>
</div>
</template>
這里就拿官方demo改了一下,可以看到,切換路由的時候,只有子路由頁面是變換的,父路由部分是沒有變換的
方式二 | 創(chuàng)建公共組件實現(xiàn)
這個方法是需要用到vuex的,當(dāng)然了,如果嫌麻煩,用storage也行
在components內(nèi)創(chuàng)建公共組件
1.在pages文件夾創(chuàng)建頁面,一個主頁,一個用戶頁面,一個活動頁面

創(chuàng)建頁面的過程就不一一細說了,具體就是文件夾下面一個index.vue,router就會讀這個index為路由指定的頁面
我們看下.nuxt文件夾下面的router.js頁面
這就是建立好的路由
2. 創(chuàng)建公共組件
這里偷個懶,用的element的導(dǎo)航欄組件
<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. 在所有路由頁面導(dǎo)入創(chuàng)建的公共組件
<template>
<div class="container">
<div>
<logo />
<h1 class="title">
nuxt-demo
</h1>
<navBar />
</div>
</div>
</template>
<script>
import Logo from '~/components/Logo.vue'
import navBar from '~/components/nav.vue'
export default {
components: {
Logo,
navBar
}
}
</script>
<style>
這樣就完成了第一步,我們看下預(yù)覽

問題出現(xiàn)了,雖然我們的路由變換了,但是導(dǎo)航欄的狀態(tài)確沒有同步,因為路由跳轉(zhuǎn)的時候,組件狀態(tài)會刷新,所以這個時候,需要共享狀態(tài),所以,我這里用的是vuex
4. 使用vuex同步導(dǎo)航欄狀態(tài)
直接在store文件夾內(nèi)進行添加就行,nuxt里推薦的兩種vuex使用方法
第一種是普通創(chuàng)建

第二種是模塊化創(chuàng)建

這里我選的是第二種方式,我也建議使用這種,因為方便維護,各種狀態(tài)一目了然
我們看下目錄結(jié)構(gòu),這里和在vue使用的vuex目錄是一樣的

這里就不一一詳細說明每個文件內(nèi)容了,本次重點是使用vuex來同步狀態(tài)
我們把狀態(tài)同步到vuex中,這樣每次頁面進來的時候,直接讀取vuex中的數(shù)據(jù),就可以同步導(dǎo)航欄狀態(tài)欄了
4.1 vuex使用報錯
store/index.js should export a method that returns a Vuex
instance.vuex在nuxt中是需要導(dǎo)出一個store實例
我們這里需要改動一下store文件下的index頁面
我們繼續(xù)回到導(dǎo)航欄組件內(nèi)
<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
// 每次切換導(dǎo)航欄,會把當(dāng)前狀態(tài)同步到vuex中
this.change_index(this.activeIndex)
}
},
created() {
if (this.barIndex) { // 判斷vuex內(nèi)是否有上一次存儲的數(shù)據(jù),有就同步到當(dāng)前狀態(tài)
this.activeIndex = this.barIndex
}
console.log('vuex', this.activeIndex)
}
}
</script>
這樣,我們就已經(jīng)可以同步導(dǎo)航欄狀態(tài)了

到此這篇關(guān)于詳解Nuxt內(nèi)導(dǎo)航欄的兩種實現(xiàn)方式的文章就介紹到這了,更多相關(guān)Nuxt內(nèi)導(dǎo)航欄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中定義src在img標(biāo)簽使用時加載不出來的解決
這篇文章主要介紹了Vue中定義src在img標(biāo)簽使用時加載不出來的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue-cli3中如何引入ECharts并實現(xiàn)自適應(yīng)
這篇文章主要介紹了Vue-cli3中如何引入ECharts并實現(xiàn)自適應(yīng),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
vue實現(xiàn)el-menu和el-tab聯(lián)動的示例代碼
本文主要介紹了vue實現(xiàn)el-menu和el-tab聯(lián)動的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

