十四、弹窗组件

目录

一、组件代码

二、使用组件


一、组件代码

(1)在src/libs/popup/index.vue中写如下代码

javascript 复制代码
<script setup>
import { useScrollLock, useVModel } from '@vueuse/core'
import { watch } from 'vue'
const props = defineProps({
  modelValue: {
    required: true,
    type: Boolean
  }
})

// const emits = defineEmits(['update:modelValue']) // 不用这个的原因是,下面的useVModel直接监听到props的变化,然后自动触发update:modelValue
const isOpen = useVModel(props)
// ----- 滚动锁定 -----
const isLocked = useScrollLock(document.body)
watch(
  isOpen,
  () => props.modelValue,
  (val) => {
    isLocked.value = val
  },
  {
    immediate: true
  }
)
</script>
<template>
  <div class="">
    <!-- teleport -->
    <teleport to="body">
      <!-- 蒙版 -->
      <transition name="fade">
        <div
          v-if="isOpen"
          class="w-screen h-screen bg-zinc-900/80 z-40 fixed top-0 left-0"
          @click="isOpen = false"
        ></div>
      </transition>
      <!-- 内容 -->
      <transition name="popup-down-up">
        <div
          v-if="isOpen"
          v-bind="$attrs"
          class="w-screen bg-white z-50 fixed bottom-0 dark:bg-zinc-800"
        >
          <slot />
        </div>
      </transition>
    </teleport>
  </div>
</template>

<style lang="scss" scoped>
// fade 展示动画
.fade-enter-active {
  transition: all 0.3s;
}

.fade-leave-active {
  transition: all 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

// popup-down-up 展示动画
.popup-down-up-enter-active {
  transition: all 0.3s;
}

.popup-down-up-leave-active {
  transition: all 0.3s;
}

.popup-down-up-enter-from,
.popup-down-up-leave-to {
  transform: translateY(100%);
}
</style>

(2)src/libs/index.js中注册该组件

javascript 复制代码
import { defineAsyncComponent } from 'vue'

export default {
  install(app) {
    const components = import.meta.glob('./*/index.vue')
    for (const [key, value] of Object.entries(components)) {
      const componentName = 'm-' + key.replace('./', '').split('/')[0]
      app.component(componentName, defineAsyncComponent(value))
    }
  }
}

二、使用组件

在src/views/main/components/navigation/mobile/index.vue中使用组件

javascript 复制代码
<!-- 汉堡按钮 -->
      <li
        ...
        @click="isOpenPopup = !isOpenPopup"
      >
        ...
      </li>

<m-popup v-model="isOpenPopup">
  <div>测试内容</div>
</m-popup>

然后就可以在这个组件的插槽里面放入相应的数据了

至此,这个弹窗组件完成!

三、在插槽中放入业务组件

这里就是展示一个弹出的菜单

(1)组件代码

src/views/main/components/menu/index.vue

javascript 复制代码
<script setup>
defineEmits(['onItemClick'])
//这里的数据是从vuex获取的,也可以从使用组件的地方传过来
</script>
<template>
  <div class="py-2 h-[80vh] flex flex-col">
    <h2 class="text-xl text-zinc-900 font-bold mb-2 px-1 dark:text-zinc-200">所有分类</h2>
    <ul class="overflow-y-scroll">
      <li
        v-for="(item, index) in $store.getters.categorys"
        :key="item.id"
        class="text-lg text-zinc-900 px-1 py-1.5 duration-100 active:bg-zinc-100 dark:text-zinc-300 dark:active:bg-zinc-900"
        @click="$emit('onItemClick', index)"
      >
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<style lang="scss" scoped></style>

(2)在popup中绑定业务组件中触发的点击事件

javascript 复制代码
    <m-popup v-model="isOpenPopup">
      <menu-vue @onItemClick="onItemClick"></menu-vue>
    </m-popup>

const onItemClick = (index) => {
  currentCategoryIndex.value = index
  isOpenPopup.value = false
}

至此,整个弹窗封装完成!

相关推荐
Lvan的前端笔记3 小时前
docker:每个前端项目一个 Nginx 容器还是只有一个Nginx容器
前端·nginx·docker
三天不学习4 小时前
Egg.js 4 突然爆火,原因是否归结于AI 原生落地需求爆发?
前端·javascript·全栈·egg.js
xcs194054 小时前
前端 vue 的前端页面debugger 进不去
前端·javascript·vue.js
明月_清风5 小时前
Deno 终局来了:从挑战 Node 到被 Cloudflare 收编
前端·后端·node.js
Csvn6 小时前
框架性能优化
前端
回眸&啤酒鸭8 小时前
【回眸】OpenSwarm 多智能体协作系统实战指南
大数据·前端·人工智能
用户69371750013848 小时前
2026,程序员的时代拐点到了
android·前端·后端
大龄秃头程序员8 小时前
一次 iBeacon + BLE 无感解锁方案的实现记录
前端
小兔子8 小时前
Python 的 GIL 与 free-threading:3.13 之后「去 GIL」走到哪一步了
前端
IT_陈寒8 小时前
SpringBoot自动配置差点让我加班到凌晨
前端·人工智能·后端