当前位置:首页 > VUE

vue实现页头页脚

2026-02-22 23:23:14VUE

实现页头和页脚的基本结构

在Vue项目中,页头和页脚通常作为全局组件存在,放置在App.vue或通过路由布局统一管理。以下是一个基础实现示例:

目录结构建议

src/
├── components/
│   ├── Header.vue
│   └── Footer.vue
├── App.vue

Header.vue 示例

<template>
  <header class="app-header">
    <nav>
      <router-link to="/">Home</router-link>
      <router-link to="/about">About</router-link>
    </nav>
  </header>
</template>

<script>
export default {
  name: 'AppHeader'
}
</script>

<style scoped>
.app-header {
  background-color: #333;
  padding: 1rem;
  color: white;
}
</style>

Footer.vue 示例

<template>
  <footer class="app-footer">
    <p>&copy; 2023 Your Company</p>
  </footer>
</template>

<script>
export default {
  name: 'AppFooter'
}
</script>

<style scoped>
.app-footer {
  background-color: #f5f5f5;
  padding: 1rem;
  text-align: center;
}
</style>

全局注册组件方式

App.vue中引入并固定显示页头页脚:

<template>
  <div id="app">
    <AppHeader />
    <router-view />
    <AppFooter />
  </div>
</template>

<script>
import AppHeader from '@/components/Header.vue'
import AppFooter from '@/components/Footer.vue'

export default {
  components: {
    AppHeader,
    AppFooter
  }
}
</script>

动态页头页脚控制

如果需要根据不同页面隐藏页脚(如登录页),可通过路由元信息控制:

vue实现页头页脚

router/index.js 配置

{
  path: '/login',
  component: Login,
  meta: { hideFooter: true }
}

App.vue 修改

<template>
  <div id="app">
    <AppHeader />
    <router-view />
    <AppFooter v-if="!$route.meta.hideFooter" />
  </div>
</template>

使用插槽增强灵活性

在需要自定义内容的场景,可以使用插槽:

Header.vue 修改

vue实现页头页脚

<template>
  <header>
    <slot name="left"></slot>
    <slot></slot> <!-- 默认插槽 -->
    <slot name="right"></slot>
  </header>
</template>

使用示例

<AppHeader>
  <template #left>
    <button @click="toggleSidebar">Menu</button>
  </template>
  <h1>Page Title</h1>
</AppHeader>

响应式设计建议

通过CSS媒体查询适配不同屏幕尺寸:

/* Header.vue 样式补充 */
@media (max-width: 768px) {
  .app-header nav {
    flex-direction: column;
  }
}

状态管理集成

当需要全局控制页头状态(如用户登录信息),可结合Vuex:

// store/modules/app.js
export default {
  state: {
    headerTitle: 'Default Title'
  },
  mutations: {
    SET_HEADER_TITLE(state, title) {
      state.headerTitle = title
    }
  }
}

Header.vue 调用

<template>
  <header>
    <h1>{{ headerTitle }}</h1>
  </header>
</template>

<script>
import { mapState } from 'vuex'

export default {
  computed: {
    ...mapState(['headerTitle'])
  }
}
</script>

标签: vue页头页脚
分享给朋友:

相关文章

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template&…

vue中实现显示和隐藏

vue中实现显示和隐藏

控制显示与隐藏的方法 在Vue中实现元素的显示和隐藏可以通过多种方式完成,以下是常见的几种方法: 1. 使用v-if指令v-if根据条件动态添加或移除DOM元素,适合需要频繁切换的场景。当条件为f…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-fu…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…