封装组件的案例-MyHeader组件

2 篇文章 0 订阅

封装组件的案例

1. 案例效果

封装要求:

    ① 允许用户自定义 title 标题

    ② 允许用户自定义 bgcolor 背景色

    ③ 允许用户自定义 color 文本颜色

    ④ MyHeader 组件需要在页面顶部进行 fixed 固定定位,且 z-index 等于 999

App.vue

<!-- <template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld msg="Hello Vue 3.0 + Vite" />
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script> -->
<template>
  <div class="app-container">
    <my-header :title="name" bgcolor="#1E90FF" color="#fff"></my-header>
  </div>
</template>
 
<script>
import MyHeader from './components/MyHeader.vue'
 
export default {
  name: 'MyApp',
  data() {
    return{
      name:'MyHeader 组件',
      isitalic:true
    }
  },
  components: {
    MyHeader,
  },
}
</script>
<style lang="less" scoped>
.app-container {
  margin-top: 45px;
  .thin{                
    font-weight: 200;      //字体变细
  }
  .italic{ 
    font-style: italic;   //倾斜字体
  }
}
</style>

 MyHeader.vue

<template>
    <div class="header-container" :style="{ backgroundColor: bgcolor, color: color }">
      {{title || 'MyHeader 组件'}}
    </div>
  </template>
   
  <script>
  export default {
    name: 'MyHeader',
    props: ['title', 'bgcolor', 'color']
  }
  </script>
   
  <style lang="less" scoped>
  .header-container {
    height: 45px;
    background-color: #1E90FF;
    text-align: center;
    line-height: 45px;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
  }
  </style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四维空间中的未知点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值