Vue後臺管理系統開發日常總結__組件PageHeader

在後臺管理系統的日常開發過程中發現對於同一個業務下面的版塊不同的開發同事每次都會重複寫頁面標題的樣式,而且不同的頁面標題還不太一樣。雖然有的頁面標題欄承載的元素不一樣,但是也有通用的部分,經過多個項目的迭代慢慢地總結與積累完善出了一個通用的頁面標題組件<PageHeader/>

下面是一個最常見的標題設計原型:

clipboard.png

下面是同事給出的封裝方案:

使用方式
<router-back class="router-back" text="詳情" />
組件封裝代碼片段
<template>
  <div>
    <a
      href="javascript:void(0)"
      :title="title"
      size="15px"
      class="font-icon arrow-left"
      @click="back"
      v-if="!disableRoute"
    ></a>
    <span
      v-show="text.length > 0 && !disableRoute"
      class="vertical-line"
    ></span>
    <span class="text">{{ text }}</span>
  </div>
</template>
<script>
export default {
  name: 'router-back',
  props: {
    text: {
      type: String,
      default: _ => ''
    },
    url: {
      type: [String, Number],
      default: _ => -1
    },
    title: {
      type: String,
      default: _ => '返回'
    },
    disableRoute: {
      type: Boolean,
      default: _ => false
    }
  },
  methods: {
    back () {
      if (typeof this.url === 'number') return this.$router.go(this.url)
      return this.$router.push(this.url)
    }
  }
}
</script>

無對比就沒有傷害,這個封裝只是爭對了單一的情況,並沒有任何擴展性和靈性性,而且在組件方法名稱和接收的屬性上有待考究。所以我果斷棄用這個組件,而選擇自己的解決方案,雖然也不是很完美,代碼質量上相比也沒有什麼大的改進,但是自我認爲還是可以分享一下。

不多廢話,先看實際效果圖:

clipboard.png

注意:截圖是在Chrome中縮小後截下的,並不是默認大小。

整個組件是通過Vue組件JSX方式寫法來實現的,我的代碼質量一般,實現上不一定是最佳的,但是我有點納悶我一個同事總是說我的多套了一些標籤,說:pageHeader還需要優化,減少標籤嵌套。下面是實現代碼:

import './pageHeader.scss'

const PageHeader = {
  name: 'PageHeader',

  props: {
    // 標題
    title: String,

    // 子標題
    subTitle: String,

    // 返回路徑,不適用於帶選項卡標題
    path: {
      type: [String, Number],
      default: -1
    },

    // 是否顯示回退按鈕
    withPath: {
      type: Boolean,
      default: false
    },

    // 子標題顯示位置 'right' | 'bottom', 不適用於帶選項卡標題
    position: {
      type: String,
      default: 'right'
    },

    // 帶選項卡標題開關
    withTab: {
      type: Boolean,
      default: false
    },

    // 選項卡是否引起路由改變
    isRoute: {
      type: Boolean,
      default: false
    },

    // 當前激活選項卡
    activeTab: {
      type: String,
      default: ''
    },

    // 選項卡數據
    options: {
      type: Array,
      default() {
        return [
          {
            title: '',
            field: '',
            path: ''
          }
        ]
      }
    }
  },

  computed: {
    isBottom() {
      return this.position === 'bottom'
    },

    curTab: {
      get: function() {
        return this.activeTab
      },

      set: function(val) {
        if (this.activeTab !== val) {
          if (this.isRoute) {
            this.options.forEach(option => {
              if (option.field === tab) {
                this.$router.push(option.path)
                this.$emit('tabChange', val)
              }
            })
          } else {
            this.$emit('tabChange', val)
          }
        }
      }
    }
  },

  methods: {
    goBack() {
      typeof this.path === 'string'
        ? this.$router.push(this.path)
        : this.$router.go(this.path)
    }
  },

  render(h) {
    const Back = (
      <div class="page-header__back">
        <el-button
          type="text"
          class="page-header__action"
          icon="el-icon-back"
          onClick={this.goBack}
        />
        <span class="page-header__separator mx__m" />
      </div>
    )

    const Header = (
      <div class="page-header-wrap">
        <div class="page-header__main">
          {this.withPath && Back}

          <div class="page-header__title">
            {(this.title || this.$slots.title) && (
              <div
                class={`page-header-title__main ${this.isBottom ? '' : 'fl'}`}
              >
                {this.$slots.title ? this.$slots.title : this.title}
              </div>
            )}

            {(this.subTitle || this.$slots.subTitle) && (
              <div
                class={`page-header-title__sub ${
                  this.isBottom ? 'lh__14' : 'fl ml__s'
                }`}
              >
                {this.$slots.subTitle ? this.$slots.subTitle : this.subTitle}
              </div>
            )}
          </div>
        </div>
        {this.$slots.action && (
          <div class={`page-header__aside ${this.isBottom ? 'lh__72' : ''}`}>
            {this.$slots.action}
          </div>
        )}
      </div>
    )

    const TabHeader = (
      <div class="page-header-wrap--tab">
        <div class="page-header-tab__main">
          <el-tabs v-model={this.curTab}>
            {this.options.map(option => (
              <el-tab-pane label={option.title} name={option.field} />
            ))}
          </el-tabs>
        </div>

        {this.$slots.extra && (
          <div class="page-header-tab__extra">{this.$slots.extra}</div>
        )}
      </div>
    )

    return (
      <div class={`page-header ${this.isBottom ? 'pt__20' : 'py__20'}`}>
        {this.withTab ? TabHeader : Header}
      </div>
    )
  }
}

export default PageHeader

上面的代碼在實現上之前沒見有考慮到通過this.$router.go(-1)回到上一個頁面,而是直接採用this.$router.push(path),這種需要傳path的方式,後來看了最前面同事寫的方案後借鑑過來,改進了一下。這個代碼實現很簡單沒有什麼需要講的,下面是組件使用的實際例子,當然如果能寫個單元測試文件來測試組件更好,但是我Jest只停留在入門水平,平時也就寫些最簡單的assert,然後過代碼覆蓋率。

由於代碼在處理選項卡時,並沒有對額外的插槽extra作處理,所以在使用時需要在對應的標籤上模擬一下<el-tabs/>下面的線。這裏直接使用了Css-in-js的一種實現styled-components的Vue版vue-styled-components,來實現在JSX中實現類似.vue中樣式的scoped功能。但是並不建議用,因爲Vue版的沒有更新,使用的人也不多,不像React社區那麼活躍。

import styled from 'vue-styled-components'
import PageHeader from '~/components/pageHeader'

const PageHeaderAction = styled.div`
  border-bottom: 2px solid #e4e7ed;
  padding-bottom: 6px;
`

const UiPageHeader = {
  name: 'UiPageHeader',
  components: {
    PageHeader
  },

  data() {
    return {
      tabActive: '01',
      tabOptions: [
        {
          title: '我的任務',
          field: '01'
        },
        {
          title: '我的流程',
          field: '02'
        },
        {
          title: '店鋪任務',
          field: '03'
        },
        {
          title: '店鋪流程',
          field: '04'
        }
      ]
    }
  },

  methods: {
    onTabChange(tab) {
      console.log(tab)
    }
  },

  render(h) {
    return (
      <div>
        <el-row>
          <PageHeader title="標題"/>
        </el-row>
        <el-row>
          <PageHeader title="標題 + 默認回退" withPath={true}/>
          <PageHeader title="標題 + 指定回退路徑" withPath={true} path="/4/dashboard"/>
        </el-row>
        <el-row>
          <PageHeader title="標題 + 右邊描述" subTitle="我是頁面標題描述文字,默認顯示在標題右邊"/>
          <PageHeader title="標題 + 下邊描述" subTitle="我是頁面標題描述文字,指定顯示在標題下邊" position="bottom"/>
          <PageHeader
            title="標題 + 回退 + 右邊描述"
            withPath={true}
            subTitle="我是頁面標題描述文字,默認顯示在標題右邊"
          />
          <PageHeader
            title="標題 + 回退 + 下邊描述"
            withPath={true}
            subTitle="我是頁面標題描述文字,指定顯示在標題下邊"
            position="bottom"
          />
        </el-row>
        <el-row>
          <PageHeader>
            <template slot="title">
              標題插槽示例
              <i class="el-icon-milk-tea"/>
              <strike style="color: #ff8e00">Yah!</strike>
            </template>
          </PageHeader>
          <PageHeader title="標題描述插槽示例">
            <template slot="subTitle">
              我是頁面標題描述文字
              <i class="el-icon-milk-tea"/>
              <strike style="color: #ff8e00">Yah!</strike>
            </template>
          </PageHeader>
          <PageHeader title="標題欄右則附加操作按鈕示例">
            <template slot="action">
              <el-button type="primary">保存</el-button>
            </template>
          </PageHeader>
          <PageHeader title="標題欄右則附加操作按鈕示例2" subTitle="我是頁面標題描述文字">
            <template slot="action">
              <el-button class="btn-link" type="text">頁面跳轉錨點</el-button>
            </template>
          </PageHeader>
          <PageHeader
            withPath={true}
            title="標題欄右則附加操作按鈕示例3"
            subTitle="我是頁面標題描述文字"
            position="bottom">
            <template slot="action">
              <el-button type="primary">保存</el-button>
            </template>
          </PageHeader>
        </el-row>
        <el-row>
          <h3>Tab選項卡標題示例</h3>
          <div>選項卡功能比較單一,只支持Element-ui默認的水平顯示</div>
          <PageHeader
            withTab={true}
            activeTab={this.tabActive}
            options={this.tabOptions}
            onTabChange={this.onTabChange}
          />
        </el-row>
        <el-row>
          <h3>選項卡 + 標題右邊附加操作按鈕</h3>
          <PageHeader
            withTab={true}
            activeTab={this.tabActive}
            options={this.tabOptions}
            onTabChange={this.onTabChange}
          >
            <template slot="extra">
              <PageHeaderAction>
                <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-plus"
                  onClick={this.onCreate}
                >
                  新建
                </el-button>
              </PageHeaderAction>
            </template>
          </PageHeader>
        </el-row>
      </div>
    )
  }
}

export default UiPageHeader

最後:寫這個的目的是爲了在工作中有所積累,寫了幾年業務系統,發現並沒有留下什麼,以文章的方式記錄是一種不錯的方式,希望能養成好習慣,堅持寫作,在寫的時候思考提升自我。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章