vue使用ElementUI

1.安装

npm i element-ui -S

2.引入

2.1完整引入

javascript 复制代码
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';

Vue.use(ElementUI);

2.2按需引入

说明:为了输入时候有提示,建议安装vue-helper

npm install babel-plugin-component -D

2.2.1创建babel.config.js

javascript 复制代码
module.exports = {
  presets: ["@vue/cli-plugin-babel/preset"],
  plugins: [
    [
      "component",
      {
        libraryName: "element-ui",
        styleLibraryName: "theme-chalk",
      },
    ],
  ],
};

2.2.2 main.js

javascript 复制代码
import Vue from 'vue';
import { Button, Select } from 'element-ui';
import App from './App.vue';

Vue.component(Button.name, Button);
Vue.component(Select.name, Select);

3.案例

说明:弹出框的实现。

3.1main.js

javascript 复制代码
// element-ui挂在原型上
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;

3.2vue

html 复制代码
<a class="btn" @click="open">立即支付</a>

3.3script

javascript 复制代码
    // 弹出框
    async open() {
      // 生成一个二维码(地址)
      let url = await QRCode.toDataURL(this.payInfo.codeUrl);

      this.$alert(`<img src=${url} />`, "请你微信支付", {
        // 	是否将 message 属性作为 HTML 片段处理,
        // 也就是展示标签的意思
        dangerouslyUseHTMLString: true,
        // 是否居中布局
        center: true,
        // 是否显示取消按钮
        showCancelButton: true,
        // MessageBox 是否显示右上角关闭按钮
        showClose: true,
        // 取消按钮的文本
        cancelButtonText: "支付遇见问题",
        // 确定按钮的文本内容
        confirmButtonText: "已支付成功",
        // 关闭弹出框的配置值
        brforeClose: (type, instance, done) => {
          // type区分取消||确定按钮
          // instance当前组件实例
          // done:关闭弹出框的方法
          if (type == "cancel") {
            alert("请联系管理员");
            // 清除定时器
            clearInterval(this.timer);
            this.timer = null;
            done();
          } else {
            // 判断是否真的支付了
            // if (this.code == 200) {
              clearInterval(this.timer);
              this.timer = null;
              done()
              // 有的时候定时器还没监听到,用户支付成功了发现没跳转 立马点了支付成功的情况。
              this.$router.push("/paysuccess");
            // }
          }
        },
      });
      // 支付成功||失败
      // 支付成功,路由跳转,如果支付失败,提示信息
      // 定时器没有,那么要开启一个定时器
      if (!this.timer) {
        this.time = setTimeout(async () => {
          // 发请求获取用户支付的状态
          let result = await this.$API.reqPayStatus(this.orderId);
          // 如果code等于200,那么表示支付已经成功了
          if (result.code == 200 || result.code == 205) {
            // 清楚定时器
            clearInterval(this.timer);
            this.timer = null;
            // 保存code
            this.code = result.code;
            // 关闭弹出框
            this.$msgbox.close();
            // 跳转到下一页路由
            this.$router.push("/paysuccess");
          }
        }, 1000);
      }
    },

3.4展示

相关推荐
明似水1 小时前
Flutter 开发入门:从一个简单的计数器应用开始
前端·javascript·flutter
沐土Arvin1 小时前
前端图片上传组件实战:从动态销毁Input到全屏预览的全功能实现
开发语言·前端·javascript
Zww08912 小时前
el-dialog鼠标在遮罩层松开会意外关闭,教程图文并茂
javascript·vue.js·计算机外设
爱编程的鱼2 小时前
C#接口(Interface)全方位讲解:定义、特性、应用与实践
java·前端·c#
sunbyte2 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | 页面布局 与 Vue Router 路由配置
前端·javascript·vue.js·tailwindcss
苹果酱05672 小时前
React方向:react脚手架的使用
java·vue.js·spring boot·mysql·课程设计
saadiya~2 小时前
Vue 3 实现后端 Excel 文件流导出功能(Blob 下载详解)
前端·vue.js·excel
摇摇奶昔x3 小时前
webpack 学习
前端·学习·webpack
阿珊和她的猫4 小时前
Vue Router中的路由嵌套:主子路由
前端·javascript·vue.js