页面小组件--标题

样例展示

  • 效果示例
  • 代码示例
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布局处理。
相关推荐
J***Q2921 小时前
Vue数据可视化
前端·vue.js·信息可视化
JIngJaneIL2 小时前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·社区互助
ttod_qzstudio2 小时前
深入理解 Vue 3 的 h 函数:构建动态 UI 的利器
前端·vue.js
_大龄3 小时前
前端解析excel
前端·excel
1***s6323 小时前
Vue图像处理开发
javascript·vue.js·ecmascript
一 乐3 小时前
应急知识学习|基于springboot+vue的应急知识学习系统(源码+数据库+文档)
数据库·vue.js·spring boot
一叶茶3 小时前
移动端平板打开的三种模式。
前端·javascript
前端大卫3 小时前
一文搞懂 Webpack 分包:async、initial 与 all 的区别【附源码】
前端
Want5953 小时前
HTML音乐圣诞树
前端·html
老前端的功夫4 小时前
前端浏览器缓存深度解析:从网络请求到极致性能优化
前端·javascript·网络·缓存·性能优化