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)文章
Vue3+axios+Mock.js實現(xiàn)登錄功能的示例代碼
本文主要介紹了Vue3+axios+Mock.js實現(xiàn)登錄功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
sublime如何配置開發(fā)VUE環(huán)境自動格式化代碼
這篇文章主要介紹了sublime如何配置開發(fā)VUE環(huán)境自動格式化代碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
jenkins自動構(gòu)建發(fā)布vue項目的方法步驟
這篇文章主要介紹了jenkins自動構(gòu)建發(fā)布vue項目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

