当前位置:首页 > VUE

vue实现布局组件

2026-01-17 22:55:17VUE

Vue 实现布局组件的方法

基础布局组件结构

使用 Vue 的单文件组件(SFC)创建一个基础布局组件。布局组件通常包含头部、侧边栏、主体内容和页脚。

<template>
  <div class="layout">
    <header class="header">
      <slot name="header"></slot>
    </header>
    <aside class="sidebar">
      <slot name="sidebar"></slot>
    </aside>
    <main class="main">
      <slot></slot>
    </main>
    <footer class="footer">
      <slot name="footer"></slot>
    </footer>
  </div>
</template>

<style scoped>
.layout {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
</style>

动态布局切换

通过 props 或 provide/inject 实现动态布局切换。例如,根据用户偏好切换不同的布局模式。

<script>
export default {
  props: {
    layoutType: {
      type: String,
      default: 'default'
    }
  },
  provide() {
    return {
      layoutType: this.layoutType
    }
  }
}
</script>

响应式布局

结合 CSS 媒体查询和 Vue 的响应式数据,实现不同屏幕尺寸下的布局调整。

<script>
import { ref, onMounted, onBeforeUnmount } from 'vue'

export default {
  setup() {
    const isMobile = ref(false)
    const checkScreenSize = () => {
      isMobile.value = window.innerWidth < 768
    }

    onMounted(() => {
      window.addEventListener('resize', checkScreenSize)
      checkScreenSize()
    })

    onBeforeUnmount(() => {
      window.removeEventListener('resize', checkScreenSize)
    })

    return { isMobile }
  }
}
</script>

<template>
  <div :class="['layout', { 'mobile-layout': isMobile }]">
    <!-- 布局内容 -->
  </div>
</template>

嵌套路由布局

在 Vue Router 中实现嵌套路由布局,使不同路由可以共享相同的布局结构。

// router.js
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '', component: Home },
      { path: 'about', component: About }
    ]
  }
]

插槽高级用法

使用作用域插槽向父组件传递布局组件的状态或方法,实现更灵活的布局控制。

<template>
  <div class="layout">
    <slot name="header" :isCollapsed="isCollapsed"></slot>
    <aside class="sidebar">
      <slot name="sidebar" :toggle="toggleSidebar"></slot>
    </aside>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapsed: false
    }
  },
  methods: {
    toggleSidebar() {
      this.isCollapsed = !this.isCollapsed
    }
  }
}
</script>

主题切换功能

通过 CSS 变量和 Vue 的状态管理,实现布局组件的主题切换功能。

vue实现布局组件

<script>
export default {
  data() {
    return {
      currentTheme: 'light'
    }
  },
  methods: {
    toggleTheme() {
      this.currentTheme = this.currentTheme === 'light' ? 'dark' : 'light'
      document.documentElement.setAttribute('data-theme', this.currentTheme)
    }
  }
}
</script>

<style>
:root {
  --bg-color: #ffffff;
  --text-color: #333333;
}

[data-theme="dark"] {
  --bg-color: #1a1a1a;
  --text-color: #f0f0f0;
}

.layout {
  background-color: var(--bg-color);
  color: var(--text-color);
}
</style>

标签: 组件布局
分享给朋友:

相关文章

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…

uniapp布局规范

uniapp布局规范

uniapp布局规范 uniapp的布局规范基于Flexbox模型,支持跨平台开发,需兼顾不同设备的适配性。以下是核心布局要点: Flex布局基础 使用Flexbox实现弹性布局,默认display…

vue实现水印组件

vue实现水印组件

Vue 水印组件的实现方法 使用Canvas绘制水印 在Vue组件中通过Canvas动态生成水印图案,将其作为背景添加到目标元素上。 <template> <div ref="…

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…

react如何刷新组件

react如何刷新组件

刷新 React 组件的常见方法 使用状态更新触发重新渲染 通过修改组件的状态(state),React 会自动触发重新渲染。例如: const [count, setCount] = useSta…