【Vue3】首页主体-面板组件封装

简介: 【Vue3】首页主体-面板组件封装

思路分析

  1. 图中标出的四个部分都是可能会发生变化的,需要我们定义为可配置
  2. 主标题和副标题由于是纯文本,我们定义成props即可
  3. 右侧内容和主体内容由于可能会传入较为复杂的自定义模板,我们定义成slot利用插槽渲染

核心代码

(1)组件编写

Home/components/home-panel.vue
<script lang="ts" setup name="HomePanel"></script>
<template>
  <div class="home-panel">
    <div class="container">
      <div class="head">
        <h3>新鲜好物<small>新鲜出炉 品质靠谱</small></h3>
        <XtxMore to="/"></XtxMore>
      </div>
      面板的内容
    </div>
  </div>
</template>
<style scoped lang="less">
.home-panel {
  background-color: #fff;
  .head {
    padding: 40px 0;
    display: flex;
    align-items: flex-end;
    h3 {
      flex: 1;
      font-size: 32px;
      font-weight: normal;
      margin-left: 6px;
      height: 35px;
      line-height: 35px;
      small {
        font-size: 16px;
        color: #999;
        margin-left: 20px;
      }
    }
  }
}
</style>

(2)props处理src/views/home/components/home-panel.vue

标题和子标题应该有父组件传递进来,不能写死

<script lang="ts" setup name="HomePanel">
defineProps({
  title: {
    type: String,
    required: true,
  },
  subTitle: {
    type: String,
    required: true,
  },
})
</script>

(3)父组件传入title和subTitle

<script lang="ts">
import HomePanel from './components/home-panel.vue'
</script>
<!-- 新鲜好物 -->
<HomePanel title="新鲜好物" subTitle="新鲜出炉 品质靠谱"></HomePanel>

(4)插槽处理,右侧的查看全部和面板具体的内容不应该写死src/views/home/components/home-panel.vue

<template>
  <div class="home-panel">
    <div class="container">
      <div class="head">
        <h3>
          {{ title }}<small>{{ subTitle }}</small>
        </h3>
+        <!-- 具名插槽 -->
+        <slot name="right"></slot>
      </div>
+      <!-- 默认的内容插槽 -->
+      <slot></slot>
    </div>
  </div>
</template>

(5)父组件修改

<!-- 新鲜好物 -->
<HomePanel title="新鲜好物" subTitle="新鲜出炉 品质靠谱">
  <template #right>
    <XtxMore to="/"></XtxMore>
  </template>
  <div>我是内容</div>
</HomePanel>
bTitle="新鲜出炉 品质靠谱">
  <template #right>
    <XtxMore to="/"></XtxMore>
  </template>
  <div>我是内容</div>
</HomePanel>
目录
相关文章
|
3天前
|
JavaScript 前端开发 CDN
vue3速览
vue3速览
12 0
|
3天前
|
设计模式 JavaScript 前端开发
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
|
3天前
|
JavaScript API
Vue3 官方文档速通(中)
Vue3 官方文档速通(中)
18 0
|
3天前
|
缓存 JavaScript 前端开发
Vue3 官方文档速通(上)
Vue3 官方文档速通(上)
20 0
|
3天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
8 1
|
3天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
7 0
|
3天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
6 0
|
3天前
|
缓存 监控 JavaScript
探讨优化Vue应用性能和加载速度的策略
【5月更文挑战第17天】本文探讨了优化Vue应用性能和加载速度的策略:1) 精简代码和组件拆分以减少冗余;2) 使用计算属性和侦听器、懒加载、预加载和预获取优化路由;3) 数据懒加载和防抖节流处理高频事件;4) 图片压缩和选择合适格式,使用CDN加速资源加载;5) 利用浏览器缓存和组件缓存提高效率;6) 使用Vue Devtools和性能分析工具监控及调试。通过这些方法,可提升用户在复杂应用中的体验。
10 0
|
4天前
|
JavaScript 前端开发
vue(1),小白看完都会了
vue(1),小白看完都会了
|
4天前
|
JavaScript 数据库
ant design vue日期组件怎么清空 取消默认当天日期
ant design vue日期组件怎么清空 取消默认当天日期

相关实验场景

更多