NafmHeader.vue 3.3 KB
<template>
  <section>
    <div class="nav_bar" :class="{bgImgClass:isBgImg}" :style="navObj.barStyle">
      <!-- 左侧 自定义 -->
      <div class="nav_left" v-if="$slots.left" slot="left">
        <slot name="left" />
      </div>
      <!-- 左侧 默认 箭头以及文字描述 -->
      <div class="nav_left" v-else @click="handleBack">
        <i v-if="navObj.barLeftArrow" class="iconfont iconpreviewleft"></i>
        {{navObj.barLeftText}}
      </div>
      <!-- 标题栏 默认 自定义 -->
      <div class="nav_title" v-if="$slots.default">
        <slot />
      </div>
      <!-- 标题栏 -->
      <div class="nav_title" v-else>{{navObj.barTitle}}</div>
      <!-- 右侧插槽 -->
      <div class="nav_right" v-if="$slots.right" slot="right">
        <slot name="right" />
      </div>
    </div>
  </section>
</template>
<script>
export default {
  name: 'NafmHeader',
  props: {
    bgColor: {
      type: String,
    },
    isBgImg: {
      type: Boolean,
      default: true
    },
    color: {
      type: String
    },
    leftText: {
      type: String
    },
    leftArrow: {
      type: Boolean,
      default: undefined
    },
    title: {
      type: String
    },
    showBack: {
      type: Boolean,
      default: false
    },
    onBack: {
      type: Function,
    }
  },
  computed: {
    navObj() {
      const meta = this.$route.meta
      let leftArrowStatus = true
      if (this.leftArrow != undefined) {
        leftArrowStatus = this.leftArrow
      } else if (meta.leftArrow != undefined) {
        leftArrowStatus = meta.leftArrow
      }

      // 如果设置了背景图片 则不用设置 背景颜色了
      // console.log('this.isBgImg',this.isBgImg)
      const barStyleBg = this.isBgImg ? {} : { background: this.bgColor || meta.bgColor || '#729ae0' }
      // console.log('meta',meta)
      return {
        barStyle: {
          ...barStyleBg,
          color: this.color || meta.color || '#fff'
        },
        barLeftText: this.leftText || meta.leftText || '',
        barTitle: this.title || meta.title || '',
        barLeftArrow: leftArrowStatus
      }
    },
  },
  methods: {
    handleBack() {
      if (this.showBack) {
        this.onBack()
      } else {
        this.$router.back()
      }
    }
  }
}
</script>
<style lang="less" scoped>
@NavHeight: 45px;
section {
  height: @NavHeight;
  width: 100%;
  position: relative;
  z-index: 10;

  .nav_bar {
    height: @NavHeight;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    padding: 0 20px;
  }
  .bgImgClass {
    background: url(../assets/icon/bg_navigation.png) !important;
    background-repeat: no-repeat;
    background-size: 100% !important;
    background-position: center center;
  }
  .nav_left {
    display: flex;
    align-items: center;
    :active {
      opacity: 0.5;
    }
    .iconpreviewleft {
      font-size: 25px;
    }
    img {
      width: 51px;
      height: 51px;
    }
  }
  .nav_title {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    z-index: -1;
  }
  .nav_right {
    min-width: 100px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>