vue使用slot封装navBar

vue使用slot封装navBar

1、创建navBar.vue文件

html 复制代码
<template>
  <div>
      <div class="headerBar">
          <div>
              <div v-if="showLeft" class="left">
                  <div v-if="leftText">{{ leftText }}</div>
                  <slot name="left" v-else ></slot>
              </div>
          </div>
          <div class="title">
              <div v-if="title">{{ title }}</div>
              <slot name="title" v-else></slot>
          </div>
          <div>
              <div v-if="showRight" class="right">
                  <div v-if="rightText"> {{ rightText }} </div>
                  <slot name="right" v-else ></slot>
              </div>
          </div>
      </div>
  </div>
</template>

<script>
export default {
    name: "index",
    props: {
        // 标题
        title: {
            type: String,
            default: '',
        },
        // 左侧显示内容
        leftText: {
            type: String,
            default: '',
        },
        // 是否显示左侧内容
        showLeft: {
            type: Boolean,
            default: true,
        },
        // 右侧内容
        rightText: {
            type: String,
            default: '',
        },
        // 是否右侧内容
        showRight: {
            type: Boolean,
            default: true,
        },
    }
}
</script>

<style scoped lang="scss">
.headerBar{
  background: #4B9EFC;
  color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 15px;
    box-sizing: border-box;

    .left{
        cursor: pointer;
        padding: 10px;
        box-sizing: border-box;
    }

    .right{
        cursor: pointer;
        padding: 10px;
        box-sizing: border-box;
    }
}
</style>

2、在页面使用页面中引入并使用

html 复制代码
<template>
  <div>
      <header-bar title="" leftText="<" right-text="我是右侧">
<!--          <template #left>
              <el-button>我是个按钮</el-button>
          </template>-->

          <template #title>
              <el-input></el-input>
          </template>

<!--          <template #right>
              <el-input></el-input>
          </template>-->
      </header-bar>
  </div>
</template>

<script>
import HeaderBar from './components/headerBar/index.vue'
export default {
    name: "index",
    components: {
        HeaderBar,
    }
}
</script>

<style scoped>

</style>
相关推荐
云小遥23 分钟前
Cornerstone3D 2.x升级调研
前端·数据可视化
李明卫杭州29 分钟前
浅谈JavaScript中Blob对象
前端·javascript
springfe010129 分钟前
Cesium 3D地图 图元 圆柱 图片实现
前端·cesium
meng半颗糖31 分钟前
vue3 双容器自动扩展布局 根据 内容的多少 动态定义宽度
前端·javascript·css·vue.js·elementui·vue3
yt9483233 分钟前
jquery和CSS3圆形倒计时特效
前端·css3·jquery
teeeeeeemo34 分钟前
CSS3 动画基础与技巧
前端·css·笔记·css3
年纪轻轻就扛不住37 分钟前
CSS3 渐变效果
前端·css·css3
Aisanyi41 分钟前
【鸿蒙开发】使用HMRouter路由的使用
前端·harmonyos
杉木笙1 小时前
Flutter 代码雨实现(矩阵雨)DLC 多图层
前端·flutter
SouthernWind1 小时前
Vista AI 演示—— 提示词优化功能
前端·vue.js