基於vue封裝的header 頭部組件


一,頭部標題動態綁定

二,頭部返回統一處理

三,頭部布局 (左,中,右) (左,右)

界面效果

   

 父組件

<template>
  <div class="profile">
    <v-header :imgSrc="imgUrl" :title="titleDec" :text="textDec"> </v-header>
  </div>
</template>

<script>
import vHeader from "../components/Header";
export default {
  name: "profile",
  components: {
    vHeader,
  },
  data() {
    return {
      imgUrl: "",
      titleDec: "我",
      textDec: "忘記密碼",
    };
  },
  created() {
    this.imgUrl = require("../assets/img/back.png");
  },
};
</script>
<style lang="less">
.active {
  color: #115ffb;
}
</style>

子組件

<template>
  <div class="header">
    <!-- 返回箭頭 -->
    <div class="header_arrow" @click="getBack">
      <slot name="header_arrow">
        <img :src="imgSrc" alt="" slot="header_arrow" />
      </slot>
    </div>
    <!-- 中間標題問題 -->
    <div class="header_title">
      <slot name="header_title">
        <span>{{ title }}</span>
      </slot>
    </div>
    <!-- 文字與圖標 -->
    <div class="header_icon">
      <slot name="header_icon">
        <span>{{ text }}</span>
      </slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "Header",
  props: {
    title: {
      type: String,
    },
    imgSrc: {
      type: String,
    },
    text: {
      type: String,
    },
  },
  methods: {
    // 返回上一頁
    getBack() {
      this.$router.back(-1);
    },
  },
};
</script>
<style lang="less">
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 45px;
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
}
// 左中右布局, 直接給中間的盒子設置flex:1;
.header .header_title {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  color: #333;
  font-weight: 700;
  margin-left: -5%;
  margin-right: -20%;
}
.header_arrow {
  width: 5%;
}
.header_arrow img {
  vertical-align: middle;
}
.header_icon {
  width: 20%;
  font-size: 14px;
  color: #115ffb;
}
</style>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM