vue3+antd 实现点击按钮弹出对话框

格式1:确认对话框

按钮:
在这里插入图片描述

点击按钮之后:
在这里插入图片描述

完整代码:

<template>
  <div>
    <a-button @click="showConfirm">Confirm</a-button>
  </div>
</template>
<script setup>
import {Modal} from "ant-design-vue";
import {createVNode} from "vue";
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

const showConfirm = () => {
  Modal.confirm({
    title: 'Do you Want to delete these items?',
    icon: createVNode(ExclamationCircleOutlined),
    content: createVNode(
        'div',
        {
          style: 'color:red;',
        },
        'Some descriptions',
    ),
    onOk() {
      console.log('OK');
    },
    onCancel() {
      console.log('Cancel');
    },
    class: 'test',
  });
};
</script>

异步确认对话框

<template>
  <div>
    <a-button @click="showPromiseConfirm">With promise</a-button>
  </div>
</template>
<script setup>
import {Modal} from "ant-design-vue";
import {createVNode} from "vue";
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

function showPromiseConfirm() {
  Modal.confirm({
    title: 'Do you want to delete these items?',
    icon: createVNode(ExclamationCircleOutlined),
    content: 'When clicked the OK button, this dialog will be closed after 1 second',

    async onOk() {
      try {
        return await new Promise((resolve, reject) => {
          setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
        });
      } catch {
        return console.log('Oops errors!');
      }
    },
    onCancel() {
    },
  });
}
</script>

删除确认对话框

<template>
  <div>
    <a-button type="dashed" @click="showDeleteConfirm">Delete</a-button>
  </div>
</template>
<script setup>
import {Modal} from "ant-design-vue";
import {createVNode} from "vue";
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

const showDeleteConfirm = () => {
  Modal.confirm({
    title: 'Are you sure delete this task?',
    icon: createVNode(ExclamationCircleOutlined),
    content: 'Some descriptions',
    okText: 'Yes',
    okType: 'danger',
    cancelText: 'No',
    onOk() {
      console.log('OK');
    },
    onCancel() {
      console.log('Cancel');
    },
  });
};
</script>

对话框的额外属性

<template>
  <div>
    <a-button type="dashed" @click="showPropsConfirm">With extra props</a-button>
  </div>
</template>
<script setup>
import {Modal} from "ant-design-vue";
import {createVNode} from "vue";
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

const showPropsConfirm = () => {
  Modal.confirm({
    title: 'Are you sure delete this task?',
    icon: createVNode(ExclamationCircleOutlined),
    content: 'Some descriptions',
    okText: 'Yes',
    okType: 'danger',

    // pass the props
    okButtonProps: {
      disabled: true,
    },
    cancelText: 'No',
    onOk() {
      console.log('OK');
    },
    onCancel() {
      console.log('Cancel');
    },
  });
};
</script>
  • 28
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,针对你的问题,我们可以通过以下步骤实现: 1. 在Vue3项目中引入Antd和Axios:在Vue3项目中,可以通过npm或yarn安装Antd和Axios,并在main.js中引入。 2. 定义章节列表和当前章节索引:在Vue3组件中,我们可以使用data属性定义章节列表和当前章节索引。 3. 通过Axios获取小说章节列表:在mounted钩子中,使用Axios发送请求获取小说章节列表,并将结果存储在章节列表中。 4. 实现切换章节的方法:在Vue3组件中定义一个方法,通过修改当前章节索引实现切换小说章节。 5. 实现按钮点击事件:在Vue3组件中,可以使用Antd的Button组件实现按钮,并绑定点击事件调用切换章节的方法。 下面是具体实现代码示例: ```vue <template> <div> <h2>{{ chapter.title }}</h2> <p>{{ chapter.content }}</p> <div> <a-button type="primary" @click="prevChapter">上一章</a-button> <a-button type="primary" @click="nextChapter">下一章</a-button> </div> </div> </template> <script> import axios from 'axios'; import { Button } from 'ant-design-vue'; export default { components: { 'a-button': Button, }, data() { return { chapters: [], // 章节列表 currentChapterIndex: 0, // 当前章节索引 }; }, mounted() { // 获取小说章节列表 axios.get('/api/chapters').then(res => { this.chapters = res.data; }); }, computed: { chapter() { return this.chapters[this.currentChapterIndex]; }, }, methods: { prevChapter() { if (this.currentChapterIndex > 0) { this.currentChapterIndex--; } }, nextChapter() { if (this.currentChapterIndex < this.chapters.length - 1) { this.currentChapterIndex++; } }, }, }; </script> ``` 以上就是一个简单的实现小说章节切换的示例代码。需要注意的是,这只是一个简单的示例,实际项目中可能需要更复杂的逻辑和交互效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python私教

创业不易,请打赏支持我一点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值