vue中router-view使用教程详解
一、介绍
router-view组件作为vue最核心的路由管理组件,在项目中作为路由管理经常被使用到。vue项目最核心的App.vue文件中即是通过router-view进行路由管理。
<template> <div id="app"> <router-view></router-view> </div> </template>
我们在自己维护项目的时候,也可以使用router-view组件进行路由管理,对于页面局部刷新的场景,该组件能发挥关键作用;以下介绍vue2项目使用router-view进行项目路由试图管理详解
二、使用方法
我们通过具体场景来介绍router-view组件用法:
1 实现效果
通过切换底部导航栏进行页面内容区域切换:
实现的功能是:点击消息、联系人、动态;页面内容进行切换;页面标题以及底部导航栏不变;
2 代码
最关键的是router.js配置:
{ path: "/routerViewPractice", name: "routerViewPractice", component: () => import("@/views/routerView/index.vue"), redirect: '/messagePage',//页面默认加载的路由 children: [ { path: "/messagePage", name: "messagePage", component: () => import("@/views/routerView/childPages/messagePage.vue") }, { path: "/contactPage", name: "contactPage", component: () => import("@/views/routerView/childPages/contactPage.vue") }, { path: "/dynamicPage", name: "dynamicPage", component: () => import("@/views/routerView/childPages/dynamicPage.vue") } ] }
文件说明:
- routerViewPractice:父路由path;
- redirect:页面router-view组件默认加载的路由;
- children:用于父页面进行切换的子路由;
vue父页面代码:
<template> <div> <title-bar :title="title" @goBack="goback"></title-bar> <router-view></router-view> <BottomBar @handleMsg='handleMsg' @lookContact='lookContact' @readDynamic='readDynamic' ></BottomBar> </div> </template> <script> import TitleBar from "@/components/TitleBar"; import BottomBar from "@/components/BottomBar"; export default { name: "", components: { TitleBar, BottomBar }, data() { return { title: "路由视图", }; }, methods: { // 返回方法 goback() { // this.$emit("GoBack"); }, handleMsg() { this.$router.push({path: '/messagePage'}) }, lookContact() { this.$router.push({path: '/contactPage'}) }, readDynamic() { this.$router.push({path: '/dynamicPage'}) } } }; </script> <style scoped> #page-title { width: 100%; background-color: #fff; display: flex; justify-content: center; } .backImg { width: 20px; } </style>
使用this.$router.push进行页面上router-view组件的路由替换;实现点击底部导航栏页面切换功能;
到此这篇关于vue中router-view使用教程详解的文章就介绍到这了,更多相关vue router-view内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Vue3 去除 vue warn 及生产环境去除console.log的方法
这篇文章主要介绍了Vue3 去除 vue warn 及生产环境去除console.log的方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-06-06
最新评论