当前位置: 首页 > article >正文

【前端样式】Sweetalert2简单用法

1、 先安装sweetalert2库:

        npm install sweetalert2

2、引用SweetAlert2 库:

        import Swal from 'sweetalert2' ;

3、代码拷过去直接去测试,vue代码

<template>
  <div>
   
    <el-button style="color: #C03639" @click="test">测试Swal文本框弹窗</el-button>
    <el-button style="color: #C03639" @click="test2">测试Swal确认操作</el-button>
    <el-button style="color: #C03639" @click="test3">测试Swal 弹窗定时关闭操作</el-button>
    <el-button style="color: #C03639" @click="test4">测试Swal 警告操作</el-button>
    <el-button style="color: #C03639" @click="test5">测试Swal 信息提示操作</el-button>
    <el-button style="color: #C03639" @click="test6">测试Swal 错误提示操作</el-button>
    <el-button style="color: #C03639" @click="test7">测试Swal 多个按钮操作</el-button>
    <el-button style="color: #C03639" @click="test8">测试Swal 自定义操作</el-button>

 </div>

</template>

<script>
import Swal from 'sweetalert2' /
export default {
    name: 'index',
   
    methods: {

         //测试Swal 文本框弹窗
    test(){
      Swal.fire({
        title: '请输入您的名字',
        input: 'text',
        inputPlaceholder: '姓名',
        showCancelButton: true,
      }).then((result) => {
        if (result.isConfirmed) {
          Swal.fire(`你好, ${result.value}!`);
        }
      });
    },
    //测试Swal 确认弹框操作
    test2(){
      Swal.fire({
        title: '确认保存?',
        text: "请确认您要保存这些更改。",
        icon: 'info',
        showCancelButton: true,
        confirmButtonText: '保存',
        cancelButtonText: '取消'
      }).then((result) => {
        if (result.isConfirmed) {
          // 执行保存操作
        }
      });

    },
    //测试Swal 弹框定时取消操作
    test3(){
      Swal.fire({
        title: '操作成功!',
        text: '数据已保存。',
        icon: 'success',
        timer: 2000,
        timerProgressBar: true,
        willClose: () => {
          console.log('弹窗将关闭');
        }
      });

    },
    //测试Swal 警告操作
    test4(){
      Swal.fire({
        title: '警告!',
        text: '您即将离开此页面,未保存的数据将丢失。',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: '继续',
        cancelButtonText: '取消'
      }).then((result) => {
        if (result.isConfirmed) {
          // 执行某个操作
        }
      });

    },
    test5(){
      Swal.fire({
        title: '操作成功!',
        text: '您的数据已成功保存。',
        icon: 'success',
        confirmButtonText: '确定'
      });


    },
    test6(){
      Swal.fire({
        title: '出错了!',
        text: '发生了一些问题,请稍后再试。',
        icon: 'error',
        confirmButtonText: '好的'
      });

    },
    test7(){
      Swal.fire({
        title: '选择一个选项',
        icon: 'question',
        showCancelButton: true,
        confirmButtonText: '选项 A',
        cancelButtonText: '选项 B',
      }).then((result) => {
        if (result.isConfirmed) {
          // 处理选项 A
        } else {
          // 处理选项 B
        }
      });

    },
    test8(){
      Swal.fire({
        title: '<strong>你说不会在爱情里犯错</strong>',
        icon: 'info',
        html: '<b>也说过会一直的爱我!</b>',
        showCloseButton: true,
        focusConfirm: false,
        confirmButtonText: '明白了',
      });

    },

    //进入页面 选择性别!
    openBox() {
      Swal.fire({
        title: '选择您的性别',
        icon: 'question',
        showCancelButton: true,
        confirmButtonText: '我是男生',
        cancelButtonText: '我是女生',
      }).then((result) => {
        if (result.isConfirmed) {
          // 处理选项 男
          Swal.fire({
            title: '哇靠,你是彭于晏!',
            imageUrl: '/images/pyy.jpg',
            confirmButtonText: '嘿嘿,别夸'
          });
        } else {
          // 处理选项 女
          Swal.fire({
            title: '哇靠,你是刘亦菲!',
            imageUrl: '/images/lyf.jpg',
            confirmButtonText: '这都被你发现啦'
          });
        }
      });
    },

   },

   
</script>

 test按钮效果:

test1按钮效果:

 

test3按钮效果:

 

test4效果:

 

test5效果:

 

test6效果:

 

test7效果:

 扩展实现:

自定义测试:

 


http://www.kler.cn/news/326699.html

相关文章:

  • 如何使用ssm实现个人日常事务管理系统+vue
  • 金融教育宣传月 | 平安养老险百色中心支公司开展金融知识“消保县域行”宣传活动
  • 心理咨询预约管理系统(含源码+sql+视频导入教程)
  • web前端与koa框架node后端实现分片断点上传
  • xtu oj 六边形
  • 制造企业如何提升项目管理效率?惠科股份选择奥博思PowerProject项目管理系统
  • Windows环境Apache httpd 2.4 web服务器加载PHP8:Hello,world!
  • 【BurpSuite】访问控制漏洞和权限提升 | Access control vulnerabilities (3-6)
  • 一个静态ip可以提取出来多少ip
  • 新版pycharm如何导入自定义环境
  • elasticsearch_exporter启动报错 failed to fetch and decode node stats
  • C语言_回调函数和qsort
  • 全局安装cnpm并设置其使用淘宝镜像的仓库地址(地址最新版)
  • [leetcode] 71. 简化路径
  • 平安养老险肇庆中心支公司开展“2024年金融教育宣传月”活动
  • 【设计模式-模板】
  • k8s StorageClass 存储类
  • 中信银行西安分行开展“担当新使命 消保县域行”金融教育宣传活动
  • 总结之Coze 是一站式 AI Bot 开发平台——工作流使用及coze总结(三)
  • vivado中除法器ip核的使用
  • VS开发 - 静态编译和动态编译的基础实践与混用
  • golang学习笔记23-面向对象(五):多态与断言【重要】
  • C++学习9.24
  • git本地分支落后于远程分支,因此推送被拒绝怎么办?
  • nodejs逐字读取文件示例
  • Python中的`super()`函数:掌握面向对象编程的艺术
  • PHP“===”的意义
  • 工具类:JWT
  • 【AI学习】Lilian Weng:Extrinsic Hallucinations in LLMs(LLM 的外在幻觉)
  • TS-AI:一种用于多模态个体化脑区划分的深度学习管道,并结合任务对比合成|文献速递-Transformer架构在医学影像分析中的应用