前言:哈喽,大家好,今天给大家分享一篇文章!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏+关注哦
️✍️️️️⚠️⬇️·正文开始
⬇️·✅❓ 0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣*️⃣#️⃣
DeepSeek 有望在技术发展上取得更多突破,如持续提升模型性能、加强多模态融合技术的研发、提高模型的可解释性和安全性等。同时,DeepSeek 的发展将对 AI 行业格局和社会生产生活方式产生更为深远的影响,推动行业的创新和发展,为人们的生活带来更多的便利和智能化体验。
已经创建好了一个基于Vue3的组合式API的项目(Composition API),并能正常运行起来,请帮我用 Vue3的组合式API(Composition API) 生成一个 模态框(Modal) 的功能组件,所有代码都保存在components/Modal 下的文件夹中。功能组件的script标签中只有setup属性,使用普通 JavaScript 实现,不使用TypeScript。
功能要有,如下属性:
visible
Boolean
false
closeOnClickOverlay
Boolean
true
closeOnEsc
Boolean
true
title
String
''
content
String
''
width
String
或 Number
'500px'
height
String
或 Number
'auto'
centered
Boolean
true
showOkButton
Boolean
true
showCancelButton
Boolean
true
okText
String
'确定'
cancelText
String
'取消'
update:visible
v-model
双向绑定,当模态框的显示状态改变时触发,通常在关闭或打开模态框时触发。Boolean
,表示新的显示状态。ok
cancel
open
close
tabindex
和 aria-label
属性。你有更好的建议也可以添加,要注明。组件定义好后给出5个及以上的调用示例,示例中添加完整的数据和事件,确保每个示例是独立的。
下面是现有目录
DeepSeekAndVue/
├── src/ # 源代码目录
│ ├── assets/ # 静态资源
│ │ ├── base.css
│ │ ├── main.css
│ │ └── logo.svg
│ ├── components/ # 组件目录
│ │ ├── HelloWorld.vue
│ │ ├── TheWelcome.vue
│ │ ├── WelcomeItem.vue
│ │ ├── Progress/
│ │ │ └── Progress.vue
│ │ ├── Accordion/
│ │ ├── BackToTop/
│ │ ├── Card/
│ │ ├── InfiniteScroll/
│ │ ├── Notification/
│ │ ├── Timeline/
│ │ ├── Switch/
│ │ ├── Tabs/
│ │ ├── Sidebar/
│ │ ├── Breadcrumbs/
│ │ ├── MasonryLayout/
│ │ ├── Rating/
│ │ ├── ColorPicker/
│ │ ├── RightClickMenu/
│ │ ├── RangePicker/
│ │ ├── Navbar/
│ │ ├── FormValidation/
│ │ ├── CopyToClipboard/
│ │ ├── ClickAnimations/
│ │ ├── ThumbnailList/
│ │ ├── KeyboardShortcuts/
│ │ ├── CommentSystem/
│ │ ├── QRCode/
│ │ ├── RadioButton/
│ │ ├── Slider/
│ │ ├── ScrollAnimations/
│ │ ├── TextInput/
│ │ ├── Divider/
│ │ ├── Checkbox/
│ │ ├── TagInput/
│ │ ├── DropdownSelect/
│ │ ├── List/
│ │ ├── Header/
│ │ ├── Footer/
│ │ ├── Pagination/
│ │ ├── FloatingActionButton/
│ │ ├── PasswordInput/
│ │ ├── GridLayout/
│ │ ├── Flexbox/
│ │ ├── Modal/
│ │ ├── DatePicker/
│ │ └── icons/
│ ├── router/ # 路由配置
│ │ └── index.js
│ ├── stores/ # Pinia 状态管理
│ │ └── counter.js
│ ├── views/ # 页面组件
│ │ ├── ProgressView.vue
│ │ ├── TabsView.vue
│ │ ├── SwitchView.vue
│ │ ├── TimelineView.vue
│ │ ├── NotificationView.vue
│ │ ├── CardView.vue
│ │ ├── InfiniteScrollView.vue
│ │ ├── BackToTopView.vue
│ │ ├── AccordionView.vue
│ │ ├── SidebarView.vue
│ │ ├── BreadcrumbsView.vue
│ │ ├── MasonryLayoutView.vue
│ │ ├── RatingView.vue
│ │ ├── ColorPickerView.vue
│ │ ├── RightClickMenuView.vue
│ │ ├── RangePickerView.vue
│ │ ├── NavbarView.vue
│ │ ├── FormValidationView.vue
│ │ ├── CopyToClipboardView.vue
│ │ ├── ClickAnimationsView.vue
│ │ ├── ThumbnailListView.vue
│ │ ├── KeyboardShortcutsView.vue
│ │ ├── CommentSystemView.vue
│ │ ├── QRCodeView.vue
│ │ ├── RadioButtonView.vue
│ │ ├── SliderView.vue
│ │ ├── ScrollAnimationsView.vue
│ │ ├── TextInputView.vue
│ │ ├── DividerView.vue
│ │ ├── CheckboxView.vue
│ │ ├── TagInputView.vue
│ │ ├── DropdownSelectView.vue
│ │ ├── ListView.vue
│ │ ├── HeaderView.vue
│ │ ├── FooterView.vue
│ │ ├── PaginationView.vue
│ │ ├── FloatingActionButtonView.vue
│ │ ├── GridLayoutView.vue
│ │ ├── PasswordInputView.vue
│ │ ├── FlexboxView.vue
│ │ ├── ModalView.vue
│ │ ├── DatePickerView.vue
│ │ └── AboutView.vue
│ ├── App.vue # 根组件
│ └── main.js # 应用入口
├── public/ # 公共资源目录
├── index.html # HTML 模板
├── package.json # 项目配置
├── vite.config.js # Vite 配置
└── node_modules/ # 依赖包目录
我将按照您的要求创建一个功能完善的Modal组件,并提供详细的调用示例。以下是组件实现:
<template>
<teleport to="body">
<transition name="modal-fade">
<div
v-if="visible"
class="modal-mask"
@click.self="handleOverlayClick"
@keydown.esc="handleEsc"
role="dialog"
aria-modal="true"
:aria-labelledby="title ? 'modal-title' : null"
>
<div
class="modal-container"
:class="{ centered }"
:style="{
width: typeof width === 'number' ? `${width}px` : width,
height: typeof height === 'number' ? `${height}px` : height
}"
>
<div class="modal-header">
<slot name="header">
<h2 v-if="title" id="modal-title" class="modal-title">{{ title }}h2>
slot>
<button
v-if="showCloseButton"
class="modal-close"
@click="closeModal"
aria-label="Close"
>
×
button>
div>
<div class="modal-content">
<slot name="content">
<div v-if="content" class="modal-default-content">{{ content }}div>
slot>
div>
<div v-if="showFooter" class="modal-footer">
<slot name="footer">
<button
v-if="showCancelButton"
class="modal-button cancel-button"
@click="handleCancel"
>
{{ cancelText }}
button>
<button
v-if="showOkButton"
class="modal-button ok-button"
@click="handleOk"
>
{{ okText }}
button>
slot>
div>
div>
div>
transition>
teleport>
template>
<script setup>
import { computed, onMounted, onBeforeUnmount } from 'vue'
const emit = defineEmits([
'update:visible',
'ok',
'cancel',
'open',
'close'
])
const props = defineProps({
visible: {
type: Boolean,
default: false
},
closeOnClickOverlay: {
type: Boolean,
default: true
},
closeOnEsc: {
type: Boolean,
default: true
},
title: {
type: String,
default: ''
},
content: {
type: String,
default: ''
},
width: {
type: [String, Number],
default: '500px'
},
height: {
type: [String, Number],
default: 'auto'
},
centered: {
type: Boolean,
default: true
},
showOkButton: {
type: Boolean,
default: true
},
showCancelButton: {
type: Boolean,
default: true
},
okText: {
type: String,
default: '确定'
},
cancelText: {
type: String,
default: '取消'
},
showCloseButton: {
type: Boolean,
default: true
}
})
const showFooter = computed(() =>
props.showOkButton || props.showCancelButton
)
const handleOverlayClick = () => {
if (props.closeOnClickOverlay) {
closeModal()
}
}
const handleEsc = (event) => {
if (props.closeOnEsc && event.key === 'Escape') {
closeModal()
}
}
const handleOk = () => {
emit('ok')
closeModal()
}
const handleCancel = () => {
emit('cancel')
closeModal()
}
const closeModal = () => {
emit('update:visible', false)
emit('close')
}
const openModal = () => {
emit('open')
}
const handleKeydown = (e) => {
if (props.visible) {
handleEsc(e)
}
}
onMounted(() => {
window.addEventListener('keydown', handleKeydown)
})
onBeforeUnmount(() => {
window.removeEventListener('keydown', handleKeydown)
})
script>
<style scoped>
.modal-fade-enter-active,
.modal-fade-leave-active {
transition: opacity 0.3s ease;
}
.modal-fade-enter-from,
.modal-fade-leave-to {
opacity: 0;
}
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
transition: opacity 0.3s ease;
}
.modal-container {
margin: auto;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
transition: all 0.3s ease;
overflow: hidden;
display: flex;
flex-direction: column;
}
.modal-container.centered {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.modal-header {
padding: 16px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
}
.modal-title {
margin: 0;
font-size: 1.25rem;
}
.modal-close {
cursor: pointer;
border: none;
background: none;
font-size: 1.5rem;
padding: 0 8px;
}
.modal-content {
padding: 16px;
flex: 1;
overflow-y: auto;
}
.modal-footer {
padding: 16px;
border-top: 1px solid #eee;
display: flex;
justify-content: flex-end;
gap: 8px;
}
.modal-button {
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
border: 1px solid #ddd;
}
.ok-button {
background-color: #007bff;
color: white;
border-color: #007bff;
}
.cancel-button {
background-color: #6c757d;
color: white;
border-color: #6c757d;
}
style>
以下是五个调用示例:
<template>
<button @click="showModal1 = true">打开基础模态框button>
<Modal v-model:visible="showModal1">
<template #header>
<h3>基础标题h3>
template>
<template #content>
<p>这是通过插槽自定义的内容p>
template>
Modal>
template>
<script setup>
import { ref } from 'vue'
import Modal from '@/components/Modal/Modal.vue'
const showModal1 = ref(false)
script>
<template>
<button @click="showModal2 = true">打开安全模态框button>
<Modal
v-model:visible="showModal2"
:close-on-click-overlay="false"
:close-on-esc="false"
title="重要操作"
content="此操作不可逆,请谨慎操作!"
/>
template>
<template>
<button @click="showModal3 = true">打开设置面板button>
<Modal
v-model:visible="showModal3"
width="800px"
height="600px"
:centered="false"
:show-cancel-button="false"
ok-text="保存"
>
<template #content>
<div class="settings-panel">
div>
template>
Modal>
template>
<template>
<button @click="showModal4 = true">打开带事件的模态框button>
<Modal
v-model:visible="showModal4"
@ok="handleOk"
@cancel="handleCancel"
@open="handleOpen"
@close="handleClose"
/>
template>
<script setup>
const handleOk = () => {
console.log('确认操作')
// 执行数据提交等操作
}
const handleCancel = () => {
console.log('取消操作')
// 执行清理操作
}
const handleOpen = () => {
console.log('模态框已打开')
// 初始化操作
}
const handleClose = () => {
console.log('模态框已关闭')
// 清理操作
}
script>
<template>
<button @click="showModal5 = true">打开自定义布局button>
<Modal v-model:visible="showModal5">
<template #header>
<div class="custom-header">
<img src="logo.png" alt="logo">
<h3>自定义头部h3>
div>
template>
<template #content>
<div class="custom-content">
<video controls src="demo.mp4">video>
div>
template>
<template #footer>
<div class="custom-footer">
<button @click="showModal5 = false">关闭button>
<button @click="submitForm">提交button>
div>
template>
Modal>
template>
补充说明:
showCloseButton
属性控制右上角关闭按钮的显示:root {
--modal-z-index: 9999;
--modal-bg-color: #fff;
--modal-border-radius: 4px;
}
建议在全局样式文件中添加以下基础样式:
/* 在 main.css 中添加 */
body.modal-open {
overflow: hidden;
padding-right: 15px; /* 防止滚动条消失导致的布局偏移 */
}
运行正常
import { createRouter, createWebHistory } from 'vue-router'
import RightClickMenuView from '../views/RightClickMenuView.vue'
import RangePickerView from '../views/RangePickerView.vue'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'progress',
component: () => import('../views/ProgressView.vue'),
},
{
path: '/tabs',
name: 'tabs',
// route level code-splitting
// this generates a separate chunk (About.[hash].js) for this route
// which is lazy-loaded when the route is visited.
// 标签页(Tabs)
component: () => import('../views/TabsView.vue'),
},
{
path: '/accordion',
name: 'accordion',
// 折叠面板(Accordion)
component: () => import('../views/AccordionView.vue'),
},
{
path: '/timeline',
name: 'timeline',
// 时间线(Timeline)
component: () => import('../views/TimelineView.vue'),
},
{
path: '/backToTop',
name: 'backToTop',
component: () => import('../views/BackToTopView.vue')
},
{
path: '/notification',
name: 'notification',
component: () => import('../views/NotificationView.vue')
},
{
path: '/card',
name: 'card',
component: () => import('../views/CardView.vue')
},
{
path: '/infiniteScroll',
name: 'infiniteScroll',
component: () => import('../views/InfiniteScrollView.vue')
},
{
path: '/switch',
name: 'switch',
component: () => import('../views/SwitchView.vue')
},
{
path: '/sidebar',
name: 'sidebar',
component: () => import('../views/SidebarView.vue')
},
{
path: '/breadcrumbs',
name: 'breadcrumbs',
component: () => import('../views/BreadcrumbsView.vue')
},
{
path: '/masonryLayout',
name: 'masonryLayout',
component: () => import('../views/MasonryLayoutView.vue')
},
{
path: '/rating',
name: 'rating',
component: () => import('../views/RatingView.vue')
},
{
path: '/datePicker',
name: 'datePicker',
component: () => import('../views/DatePickerView.vue')
},
{
path: '/colorPicker',
name: 'colorPicker',
component: () => import('../views/ColorPickerView.vue')
},
{
path: '/rightClickMenu',
name: 'rightClickMenu',
component: RightClickMenuView
},
{
path: '/rangePicker',
name: 'rangePicker',
component: () => import('../views/RangePickerView.vue')
},
{
path: '/navbar',
name: 'navbar',
component: () => import('../views/NavbarView.vue')
},
{
path: '/formValidation',
name: 'formValidation',
component: () => import('../views/FormValidationView.vue')
},
{
path: '/copyToClipboard',
name: 'copyToClipboard',
component: () => import('../views/CopyToClipboardView.vue')
},
{
path: '/clickAnimations',
name: 'clickAnimations',
component: () => import('../views/ClickAnimationsView.vue')
},
{
path: '/thumbnailList',
name: 'thumbnailList',
component: () => import('../views/ThumbnailListView.vue')
},
{
path: '/keyboardShortcuts',
name: 'keyboardShortcuts',
component: () => import('../views/KeyboardShortcutsView.vue')
},
{
path: '/commentSystem',
name: 'commentSystem',
component: () => import('../views/CommentSystemView.vue')
},
{
path: '/qRCode',
name: 'qRCode',
component: () => import('../views/QRCodeView.vue')
},
{
path: '/radioButton',
name: 'radioButton',
component: () => import('../views/RadioButtonView.vue')
},
{
path: '/slider',
name: 'slider',
component: () => import('../views/SliderView.vue')
},
{
path: '/scrollAnimations',
name: 'scrollAnimations',
component: () => import('../views/ScrollAnimationsView.vue')
},
{
path: '/textInputView',
name: 'textInputView',
component: () => import('../views/TextInputView.vue')
},
{
path: '/divider',
name: 'divider',
component: () => import('../views/DividerView.vue')
},
{
path: '/checkbox',
name: 'checkbox',
component: () => import('../views/CheckboxView.vue')
},
{
path: '/tagInput',
name: 'tagInput',
component: () => import('../views/TagInputView.vue')
},
{
path: '/dropdownSelect',
name: 'dropdownSelect',
component: () => import('../views/DropdownSelectView.vue')
},
{
path: '/list',
name: 'list',
component: () => import('../views/ListView.vue')
},
{
path: '/header',
name: 'header',
component: () => import('../views/HeaderView.vue')
},
{
path: '/footer',
name: 'footer',
component: () => import('../views/FooterView.vue')
},
{
path: '/pagination',
name: 'pagination',
component: () => import('../views/PaginationView.vue')
},
{
path: '/floatingActionButton',
name: 'floatingActionButton',
component: () => import('../views/FloatingActionButtonView.vue')
},
{
path: '/gridLayout',
name: 'gridLayout',
component: () => import('../views/GridLayoutView.vue')
},
{
path: '/passwordInput',
name: 'passwordInput',
component: () => import('../views/PasswordInputView.vue')
},
{
path: '/flexbox',
name: 'flexbox',
component: () => import('../views/FlexboxView.vue')
},
{
path: '/modal',
name: 'modal',
component: () => import('../views/ModalView.vue')
}
],
})
export default router
<script setup>
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
script>
<template>
<header>
<img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" />
<div class="wrapper">
<HelloWorld msg="You did it!" />
<nav>
<RouterLink to="/">ProgressRouterLink>
<RouterLink to="/tabs">TabsRouterLink>
<RouterLink to="/accordion">AccordionRouterLink>
<RouterLink to="/timeline">TimelineRouterLink>
<RouterLink to="/backToTop">BackToTopRouterLink>
<RouterLink to="/notification">NotificationRouterLink>
<RouterLink to="/card">CardRouterLink>
<RouterLink to="/infiniteScroll">InfiniteScrollRouterLink>
<RouterLink to="/switch">SwitchRouterLink>
<RouterLink to="/sidebar">SidebarRouterLink>
<RouterLink to="/breadcrumbs">BreadcrumbsRouterLink>
<RouterLink to="/masonryLayout">MasonryLayoutRouterLink>
<RouterLink to="/rating">RatingRouterLink>
<RouterLink to="/datePicker">DatePickerRouterLink>
<RouterLink to="/colorPicker">ColorPickerRouterLink>
<RouterLink to="/rightClickMenu">RightClickMenuRouterLink>
<RouterLink to="/rangePicker">RangePickerRouterLink>
<RouterLink to="/navbar">NavbarRouterLink>
<RouterLink to="/formValidation">FormValidationRouterLink>
<RouterLink to="/copyToClipboard">CopyToClipboardRouterLink>
<RouterLink to="/clickAnimations">ClickAnimationsRouterLink>
<RouterLink to="/thumbnailList">ThumbnailListRouterLink>
<RouterLink to="/keyboardShortcuts">KeyboardShortcutsRouterLink>
<RouterLink to="/commentSystem">CommentSystemRouterLink>
<RouterLink to="/qRCode">QRCodeRouterLink>
<RouterLink to="/radioButton">RadioButtonRouterLink>
<RouterLink to="/slider">SliderRouterLink>
<RouterLink to="/scrollAnimations">ScrollAnimationsRouterLink>
<RouterLink to="/textInputView">TextInputRouterLink>
<RouterLink to="/divider">DividerRouterLink>
<RouterLink to="/checkbox">CheckboxRouterLink>
<RouterLink to="/tagInput">TagInputRouterLink>
<RouterLink to="/dropdownSelect">DropdownSelectRouterLink>
<RouterLink to="/list">ListRouterLink>
<RouterLink to="/header">HeaderRouterLink>
<RouterLink to="/footer">FooterRouterLink>
<RouterLink to="/pagination">PaginationRouterLink>
<RouterLink to="/floatingActionButton">FloatingActionButtonRouterLink>
<RouterLink to="/gridLayout">GridLayoutRouterLink>
<RouterLink to="/passwordInput">PasswordInputRouterLink>
<RouterLink to="/flexbox">FlexboxRouterLink>
<RouterLink to="/modal">ModalRouterLink>
nav>
div>
header>
<RouterView />
template>
<style scoped>
header {
line-height: 1.5;
max-height: 100vh;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
nav {
width: 100%;
font-size: 12px;
text-align: center;
margin-top: 2rem;
}
nav a.router-link-exact-active {
color: var(--color-text);
}
nav a.router-link-exact-active:hover {
background-color: transparent;
}
nav a {
display: inline-block;
padding: 0 1rem;
border-left: 1px solid var(--color-border);
}
nav a:first-of-type {
border: 0;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
}
nav {
text-align: left;
margin-left: -1rem;
font-size: 1rem;
padding: 1rem 0;
margin-top: 1rem;
}
}
style>
到此这篇文章就介绍到这了,更多精彩内容请关注本人以前的文章或继续浏览下面的文章,创作不易,如果能帮助到大家,希望大家多多支持宝码香车~,若转载本文,一定注明本文链接。
更多专栏订阅推荐:
html+css+js 绚丽效果
vue
✈️ Electron
⭐️ js
字符串
✍️ 时间对象(Date())操作