vue之配置路由和layout布局实践
更新时间:2026年07月01日 09:53:41 作者:小小liang
这篇文章主要介绍了vue之配置路由和layout布局实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
配置路由

router/index.js
- 基础的路由配置
- 还有登录权限的配置
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [{
path: "/login",
name: "Login",
component: () => import("../views/login/Login.vue")
}, {
path: "/",
name: "Layout",
component: () => import("../views/layout/index.vue"),
children: [{
//默认 显示home组件
path: "",
name: "Home",
component: () => import("../views/home/Home.vue")
}, {
path: "/home",
name: "Home",
component: () => import("../views/home/Home.vue")
},
{
path: "/qa",
name: "Qa",
component: () => import("../views/qa/Qa.vue")
}, {
path: "/video",
name: "Video",
component: () => import("../views/video/Video.vue")
}, {
path: "/me",
name: "Me",
component: () => import("../views/me/me.vue")
}
]
}]
const router = new VueRouter({
routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}
export default router
layout/index.js
- 在view 下创建 layout 文件 这个是用于布局的文件
- 然后引入 设置路由的显示 和 tabber
- 刷新后,还想高亮之前的tab中的item
<template>
<div class="layout-container">
<!-- 子路由出口 -->
<router-view />
<!-- 底部导航栏 -->
<van-tabbar v-model="active">
<van-tabbar-item v-for="item in tabbar" @click="goto(item.path)" :key="item.path" :icon="item.icon">{{ item.text }}</van-tabbar-item>
</van-tabbar>
</div>
</template>
<script>
import Vue from 'vue';
import {
Tabbar,
TabbarItem
} from 'vant';
Vue.use(Tabbar);
Vue.use(TabbarItem);
export default {
name: 'Layout',
data() {
return {
active: 0,
tabbar:[
{
path:"/home",
text:"首页",
icon:"home-o"
},
{
path:"/qa",
text:"问答",
icon:"search"
},
{
path:"/video",
text:"视频",
icon:"volume-o"
}, {
path:"/me",
text:"我的",
icon:"friends-o"
}
]
};
},
created(){
this.tabbar.map((item,idx) =>{
if(item.path === this.$route.path){
this.active = idx;
}
})
},
methods: {
goto(path){
this.$router.push(path)
}
},
components: {},
}
</script>
<style lang="scss" scoped>
</style>
router/index.js
{
path: '/',
redirect: '/XXX',
},
{
path: "/",
name: "Layout",
component: () => import("../views/layout/index.vue"),
children: [{
//默认 显示home组件
path: "",
name: "Home",
meta:{
keepalive:true
},
component: () => import("../views/home/Home.vue")
}, {
path: "/home",
name: "Home",
component: () => import("../views/home/Home.vue")
},
{
path: "/qa",
name: "Qa",
component: () => import("../views/qa/Qa.vue")
}, {
path: "/video",
name: "Video",
component: () => import("../views/video/Video.vue")
}, {
path: "/me",
name: "Me",
component: () => import("../views/me/me.vue")
}
]
},
- 骚操作
- 控制底下的tabbar是否显示


总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Vuejs第一篇之入门教程详解(单向绑定、双向绑定、列表渲染、响应函数)
组件(Component)是 Vue.js 最强大的功能之一。接下来给大家介绍vuejs单向绑定、双向绑定、列表渲染、响应函数的相关知识,非常不错,感兴趣的朋友一起看看吧2016-09-09
vite+vue3项目报错:TypeError: Promise.allSettled is not a fu
Vite+Vue3项目中遇到“TypeError: Promise.allSettled is not a function”错误,通常是因为当前运行的JavaScript环境不支持Promise.allSettled,下面就来介绍几种解决方法,感兴趣的可以了解一下2024-12-12
vue代理如何配置重写方法(pathRewrite与rewrite)
这篇文章主要介绍了vue代理如何配置重写方法(pathRewrite与rewrite),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-03-03


最新评论