页面小组件--标题

样例展示

  • 效果示例
  • 代码示例
html 复制代码
<page-header />

<page-header>
  <template slot="right">
    <el-button
      type="primary"
      icon="el-icon-delete"
      @click="handleOperateData({},'publish')"
    >新增</el-button>
  </template>
</page-header> 

组件源码

html 复制代码
<template>
  <div class="page-header">
    <!--左侧标题区域-->
    <div class="header-left">
      <!-- 利用插槽功能支持自定义标题区域内容,否则展示默认样式 -->
      <slot name="left">
        <div class="header-line"></div>
        <div class="header-title">{{ title }}</div>
      </slot>
    </div>
    <!--右侧扩展区域-->
    <div class="header-right">
      <slot name="right" />
    </div>
    <div class="header-bottom-line"></div>
  </div>
</template>
<script>
export default {
  name: 'pageHeader',
  props: {
    pageTitle: String
  },
  computed: {
    title () {
      // 默认展示路由配置中的标题,也可由组件属性传入
      return this.pageTitle || this.$route.meta?.title
    }
  }
}
</script>
<style lang="scss" scoped>
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  padding-bottom: 6px;
  margin-bottom: 15px;
  background-color: #fff;
  .header-left {
    display: flex;
    align-items: center;

    .header-line {
      width: 8px;
      height: 24px;
      margin-right: 10px;
      background: #2796d4;
    }

    .header-title {
      color: #33354a;
      font-size: 20px;
      font-weight: bold;
      line-height: 52px;
    }
  }

  .header-right {
    cursor: pointer;
  }

  .header-bottom-line {
    position: absolute;
    bottom: 0px;
    width: 100%;
    height: 1px;
    background-color: #ccc;
  }
}
</style>

组件说明

  • left 具名插槽
    • 支持自定义内容,也可使用默认内容展示
    • 用的不多,项目中使用大多是要求标题样式展示为统一样式,很少会有自定义标题展示的,聊胜于无。个人初衷是为了更加通用而已。
  • right 具名插槽
    • 用的很多,页面中属于功能扩展的按钮都可以放到这里,如:新增、导入、导出、批量删除...
    • 后续在项目使用中发现一个问题(没改,个人认为应该归到插槽区域内容处理):如果插槽区域内放置的都是按钮(el-button,行内元素或行内块元素类型),展示是正常的。但如果放置的是其他类型的内容(其他组件或自定义内容,为块状元素类型),因其独占一行,可能会导致显示异常。如下:

      原因是导入按钮其实是el-upload组件改造的,导致出现这种情况。但个人觉得可以在插槽内用div包裹+flex布局处理。
相关推荐
问心无愧05136 小时前
ctf show web入门160 161
前端·笔记
李小白666 小时前
第四天-WEB服务器基本原理,IIS服务
运维·服务器·前端
humcomm7 小时前
AI编程时代新前端职位
前端·ai编程
好家伙VCC7 小时前
Web Components主题热切换方案揭秘
java·前端
甲维斯8 小时前
Kimi版超级玛丽效果“惊人”,配额不足5厘米!
前端·人工智能
hboot8 小时前
AI工程师第一课 - Python
前端·后端·python
凉菜凉凉8 小时前
AI时代,被抛弃的前端
前端·ai
console.log('npc')8 小时前
AI前端工程与生成式UI学习路线
前端·人工智能·ui
梦曦i8 小时前
uni-router v1.1.1发布:守卫超时保护+路由监听
前端·uni-app
qq_2518364579 小时前
基于java Web网络订餐系统设计与实现 源码文档
java·开发语言·前端