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>
相关推荐
Highcharts.js11 分钟前
Highcharts Dashboards 官方文档|如何创建第一个看板
前端·开发文档·仪表板·highcharts·创建看板
小明记账簿18 分钟前
解锁前端新技能:让JavaScript与CSS变量共舞
前端·javascript·css
pyniu22 分钟前
redis day1
java·前端·spring
b***748830 分钟前
从技术复杂度到体系竞争力:2026 年前端发展的全新范式转移
前端
IT_陈寒33 分钟前
Java并发编程避坑指南:这5个隐藏陷阱让你的性能暴跌50%!
前端·人工智能·后端
化作繁星34 分钟前
前端设计模式详解
前端·设计模式
jinxinyuuuus42 分钟前
快手在线去水印:短链解析、API逆向与视频流的元数据重构
前端·人工智能·算法·重构
棒棒的唐1 小时前
avue uploader图片预览拉伸变型的css处理方法
前端·css
sunshine~~~1 小时前
ROS 2 Jazzy + Python 3.12 + Web 前端案例
开发语言·前端·python·anaconda·ros2
古韵1 小时前
如何从Axios平滑迁移到Alova
vue.js·react.js·node.js