Vue3+Antd实现弹框显示内容并加入复制按钮

使用Vue3+antd实现点击弹框出现内容并可复制内容的功能:

HTML部分:

<a-button type="primary" @click="showModel">
  打开弹框
</a-button>

<!-- @ok 是弹框中确定按钮的操作,@cancel 是弹框中取消按钮的操作 -->
<a-modal
    title="内容如下"
    :visible="isModalVisible"
    @ok="handleOk"
    @cancel="handleCancel"
>
  <div  style="display: flex; flex-direction: column; align-items: center;">
    <p>弹框内容</p>
    <a-button type="primary" @click="copyContent">复制</a-button>
  </div>
</a-modal>

JS部分:

  import {message} from "ant-design-vue";
  
  const isModalVisible = ref(false)

  // 打开弹框
  const showModel = () => {
    isModalVisible.value = true
  }
  // 弹框中取消按钮
  const handleCancel = () => {
    isModalVisible.value = false;
  }
  // 弹框中复制按钮
  const copyContent = () => {
    const textToCopy = '弹框内容';    // 弹框内容,即<p>中的内容
    navigator.clipboard.writeText(textToCopy).then(() => {
      message.success("复制成功")
      console.log('Text copied to clipboard');
    }).catch((err) => {
      message.warning("复制失败")
      console.error('Unable to copy text to clipboard', err);
    });
  }
  // 弹框中确定按钮
  const handleOk = () => {
    isModalVisible.value = false;
  };

以上代码弹框是有两个按钮:取消、确认。

如何实现只有一个取消/确认按钮

新增::footer="null" 即可取消掉两个按钮,但是要手动加入按钮:

<a-modal
    title="内容如下"
    :visible="isModalVisible"
    :footer="null"
>
  <div  style="display: flex; flex-direction: column; align-items: center;">
    <p>弹框内容</p>
    <a-button type="primary" @click="copyContent">复制</a-button>
  </div>
  <div align="right">
    <a-button type="default" align="right" @click="handleCancel">取消</a-button>
  </div>
</a-modal>

修改确认/取消按钮位置样式等

<a-button type="primary" @click="showModel">
  打开弹框
</a-button>

<!-- @ok 是弹框中确定按钮的操作,@cancel 是弹框中取消按钮的操作 -->
<a-modal
    title="内容如下"
    :visible="isModalVisible"
    @ok="handleOk"
    @cancel="handleCancel"
>

  <template #okText>
    修改'确认'按钮中的文本
  </template>
  <template #cancelText>
    修改'取消'按钮中的文本
  </template>
  <template #footer>
    自定义按钮位置样式等
  </template>
  <template #closeIcon>
    修改弹框右上角'x'的样式
  </template>

  <div  style="display: flex; flex-direction: column; align-items: center;">
    <p>弹框内容</p>
    <a-button type="primary" @click="copyContent">复制</a-button>
  </div>
</a-modal>

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值