Vue+Router+Element實現(xiàn)簡易導(dǎo)航欄
本項目為大家分享了Vue+Router+Element實現(xiàn)簡易導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
項目結(jié)構(gòu):

直接上代碼:主要就是引入配置路由Router
①:引入Router(路由管理器)
//config.js 頁面
//導(dǎo)航欄
import Home from '../components/home'
//首頁
import Index from '../components/index'
//視頻平臺
import Vid from '../components/vid_terrace'
//其他頁面
import Rests from '../components/rests'
export default {
routes:[
{
path:'/home',
name: 'home',
component: Home,
},
{
/**
* home 配置的就是導(dǎo)航欄,這個必須配置不然就會跳轉(zhuǎn)到新的頁面
* /home/index
*/
path: '/home',
name: 'home',
component: Home,
redirect: 'index',
children: [
{
name: 'index',
path: '/index',
component: Index
},
{
name: 'vid',
path: '/vid',
component: Vid
},
{
name:'rests',
path: '/rests',
component: Rests
}
]
}
],
// 去掉Vue地址的#
mode:'history'
}
//index.js 頁面
import VueRouter from "vue-router"; import Vue from "vue"; import Config from './config'; Vue.use(VueRouter); let router = new VueRouter(Config); export default router;
//main.js 頁面
import Vue from 'vue';
import App from './App';
// 引入Element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//引入./router/index文件
import router from "./router/index";
Vue.config.productionTip = false
Vue.use(ElementUI);
new Vue({
el: '#app',
render: h => h(App),
router
})
//app.vue 頁面
<template>
<div id="app">
<!-- 組件是一個 functional 組件,渲染路徑匹配到的視圖組件。-->
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
components: {
}
}
</script>
<style>
#app {
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
//home.vue 頁面
<template>
<!-- 導(dǎo)航欄-->
<div>
<el-menu
:default-active="this.$route.path"
class="el-menu-demo"
mode="horizontal"
@select="handleSelect"
router
background-color="#545c64"
text-color="#fff"
active-text-color="#ffd04b">
<el-menu-item v-for="(tit,i) in titleList" :key="i" :index="tit.name">
<template>{{ tit.navItem }}</template>
</el-menu-item>
</el-menu>
<el-main class="detailed-content">
<router-view />
</el-main>
</div>
</template>
<script>
export default {
data() {
return {
activeIndex: '1',
activeIndex2: '1',
titleList:[
{name:'index', navItem:'首頁'},
{name:'vid',navItem:'視頻平臺'},
{name:'rests',navItem:'其他'},
]
}
},
methods: {
handleSelect(key, keyPath) {
console.log(key, keyPath);
}
}
}
</script>
<style scoped>
</style>
效果圖:

乍一看可能有點繁瑣,因為Router的配置有點亂,其實導(dǎo)航欄的代碼沒幾行,如果你的環(huán)境已經(jīng)搭好那就只需要看下home.vue和config.js文件就好。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用vuedraggable實現(xiàn)從左向右拖拽功能
這篇文章主要為大家詳細(xì)介紹了使用vuedraggable實現(xiàn)從左向右拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Vue+element+cookie記住密碼功能的簡單實現(xiàn)方法
這篇文章主要給大家介紹了Vue+element+cookie記住密碼功能的簡單實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
vue項目中做編輯功能傳遞數(shù)據(jù)時遇到問題的解決方法
這篇文章主要介紹了vue項目中做編輯功能傳遞數(shù)據(jù)時遇到問題的解決方法,vue父組件向子組件傳遞數(shù)據(jù)的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端)
這篇文章主要介紹了使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
vue 自定指令生成uuid滾動監(jiān)聽達到tab表格吸頂效果的代碼
這篇文章主要介紹了vue 自定指令生成uuid滾動監(jiān)聽達到tab表格吸頂效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
vue3(optionApi)使用Element Plus庫沒有效果的解決方式
這篇文章主要介紹了vue3(optionApi)使用Element Plus庫沒有效果的解決方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

