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>
相关推荐
fanruitian7 分钟前
python 进程 线程
java·前端·python
ZoeLandia12 分钟前
React03:useState 与不可变更新
前端·react.js·typescript·react
蒲公英内测分发27 分钟前
智能手表卖出去后,包装、说明书和官网里的 App 下载入口怎么统一?
前端·人工智能·测试工具·智能家居·智能硬件·智能手表
溪语流沙35 分钟前
【Web全栈进阶】监控与告警:线上系统的“体检报告”
前端·vue.js·python·fastapi
鬼手点金40 分钟前
单轮对话:最简单的 API 调用
java·服务器·前端·javascript·react.js·servlet·tomcat
燕落南枝1 小时前
CSS 字体样式:颜色、字体族、倾斜、加粗与装饰
前端·css
鬼手点金1 小时前
消息的四种角色
java·前端·人工智能·git·深度学习
陈随易1 小时前
副业搞钱之代办永不过时
前端·后端·程序员
古夕1 小时前
同样的 font-weight,为什么这个项目的字体就是更细?
前端·javascript
程序员20071 小时前
抽卡玄学?不,是数学:保底机制下出货概率到底怎么算
前端