vue路由配置公共布局layout

本篇实现三段式界面
在这里插入图片描述

公共布局文件

首先在src下新建layoutPc文件夹,再给layoutPc新建组件 header 、bottm、main三个文件基本确定了一个页面的基本架子,然后再新建一个index.vue文件
在这里插入图片描述
comment/AppMain.vue

<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
      <keep-alive >
        <router-view v-if="!$route.meta.link" :key="key" />
      </keep-alive>
    </transition>
    <iframe-toggle />
  </section>
</template>

<script>

export default {
  name: 'AppMain',
  components: {  },
  computed: {
    key() {
      return this.$route.path
    }
  }
}
</script>

bootom/index.vue

<template>
  <div class="bottom-wrapper">
    <el-row>
      <el-col :span="8">
        <div>
          友情链接
        </div>
      </el-col>
      <el-col :span="8">
        <p>© {{ new Date().getFullYear() }} </p>
        <div>
          <div class="web-title">
            <svg-icon icon-class="web" size="1.1875rem"></svg-icon>
            网站资讯
          </div>

        </div>

      </el-col>
      <el-col :span="8">
        <div>
          广告位招租
        </div>

      </el-col>
    </el-row>

  </div>
</template>

<script>

export default {
  name: "bottom",
  data() {
    return {
    }
  },
  computed:{

  },
  mounted() {

  },
  methods:{

  }
};
</script>

header/index.vue

<template>
  <div class="header-wrapper show up">
    headers
  </div>
</template>

<script>

export default {
  name: "headers",
  data() {
    return {
    }
  },
  components: {
  },

};
</script>

layoutPc/index.vue
引入三个组件

<template>
  <div >
    <Header/>
    <app-main/>
    <Bottom/>
  </div>
</template>
<script setup>
import Header from "@/layoutPc/header/index";
import Bottom from "@/layoutPc/bottom/index";
import AppMain from "@/layoutPc/comment/AppMain";
export default {
  name: "layoutPc",
  components: {
    Header,
    Bottom,
    AppMain
  },
}
</script>

router/index.js

路由配置

import Layout from '@/layoutPC/index'

export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/index',
        component: () => import('@/views/index.vue')
      },
    ]
  },
 ]

效果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,可以通过使用`createRouter`函数来创建路由实例,同时使用`createWebHashHistory`或`createWebHistory`函数来创建不同类型的路由模式。在动态添加路由的过程中,需要使用`router.addRoute`方法向路由器中添加新的路由配置项。 下面是一个示例代码,展示如何使用Vue3、Vite和Layout布局动态添加路由: ```javascript // 引入相关模块 import { createRouter, createWebHashHistory } from 'vue-router'; import Layout from '@/layout/index.vue'; // 创建路由实例 const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', component: Layout, children: [] } ] }); // 添加动态路由 const newRoutes = [ { path: '/dashboard', component: () => import('@/views/dashboard/index.vue'), name: 'Dashboard', meta: { title: 'Dashboard', icon: 'dashboard' } }, { path: '/example', component: Layout, redirect: '/example/table', name: 'Example', meta: { title: 'Example', icon: 'example' }, children: [ { path: 'table', name: 'Table', component: () => import('@/views/table/index.vue'), meta: { title: 'Table', icon: 'table' } }, { path: 'tree', name: 'Tree', component: () => import('@/views/tree/index.vue'), meta: { title: 'Tree', icon: 'tree' } } ] } ]; newRoutes.forEach(route => { router.addRoute(route); }); export default router; ``` 在上面的示例代码中,我们首先创建了一个包含一个空子路由路由配置项。然后,我们定义了一个数组变量`newRoutes`,用于存放需要动态添加的路由配置项。最后,我们通过`forEach`循环遍历`newRoutes`数组,并通过`router.addRoute`方法将每个路由配置项添加到路由器中。 需要注意的是,`router.addRoute`方法只能在初始化路由时使用,无法在路由已经初始化之后使用。因此,我们需要在`createRouter`函数中初始化路由,并在路由创建完成之后再添加动态路由
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值