当前位置: 代码网 > it编程>编程语言>Javascript > vue3实现alert自定义的plugins方式

vue3实现alert自定义的plugins方式

2024年09月08日 Javascript 我要评论
vue3实现alert自定义的plugins技术环境:vue3实现一个alert的plugins代码目录如下:1. plugins/message/index.vue<template>

vue3实现alert自定义的plugins

技术环境:

vue3实现一个alert的plugins

代码目录如下:

1. plugins/message/index.vue

<template>
  <div class="default-message">
    <div class="default-message-content">
      <div class="default-message-title">{{title}}</div>
      <div class="default-message-value" v-html="content"></div>
      <div class="default-message-btns">
        <div class="default-message-cancle default-message-btn"
          v-if="cancletext" :style="setcanclecolor()"
          @click.prevent.stop="handlecancel">
          {{cancletext}}
        </div>
        <div class="default-message-submit default-message-btn"
          :style="setokcolor()" @click.prevent.stop="handleok">
          {{oktext}}
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { definecomponent } from "vue";
export default definecomponent({
  name: "message",
  props: {
    // 标题
    title: {
      type: string,
      default: "提示",
    },
    // 内容
    content: {
      type: string,
      default: "",
      required: true,
    },
    // 确定按钮文字
    oktext: {
      type: string,
      default: "确定",
    },
    // 确定按钮文字颜色
    oktextcolor: {
      type: string,
      default: "#26a2ff",
    },
    // 取消按钮文字
    cancletext: {
      type: string,
      default: "",
    },
    // 取消按钮文字颜色
    cancletextcolor: {
      type: string,
      default: "#999",
    },
    // 成功回调
    success: {
      type: function,
    },
    // 失败回调
    fail: {
      type: function,
    },
  },
  setup(props) {
    // ok的颜色
    const setokcolor = () => {
      return `color: ${props.oktextcolor}`;
    };
    // 取消的颜色
    const setcanclecolor = () => {
      return `color: ${props.cancletextcolor}`;
    };

    // 移除当前组件
    function removemodal() {
      const modeldom = document.body.queryselector(
        `.__default__container__message__`
      );
      if (modeldom) {
        document.body.removechild(modeldom);
      }
    }

    const handlecancel = () => {
      removemodal();
      props.fail && props.fail();
    };

    const handleok = () => {
      removemodal();
      props.success && props.success();
    };

    return {
      setokcolor,
      setcanclecolor,

      handleok,
      handlecancel,
    };
  },
});
</script>
<style scoped lang="scss">
.default-message {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 1000;
  background: rgba($color: #000000, $alpha: 0.3);

  .default-message-title {
    padding-top: 15px;
    text-align: center;
    font-size: 18px;
    font-weight: 700;
    color: #333;
  }

  .default-message-content {
    width: 85%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
    background-color: #fff;
    border-radius: 6px;
    transition: all 0.2s ease-in;
    color: #999;
    font-size: 18px;
  }

  .default-message-value {
    padding: 10px 20px 15px;
    min-height: 36px;
    position: relative;
    color: #999;
    text-align: center;
    line-height: 36px;
  }
  .default-message-btns {
    // border-top: 1px solid #ddd;
    display: flex;
    height: 46px;
    position: relative;
    &:after {
      position: absolute;
      content: "";
      display: inline-block;
      left: 0;
      right: 0;
      top: 0;
      height: 1px;
      transform: scaley(0.5);
      background: #ddd;
    }
    .default-message-btn {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 16px;
      padding: 0 3px;
    }
    .default-message-cancle {
      position: relative;
      &:after {
        position: absolute;
        content: "";
        display: inline-block;
        top: 0;
        right: 0;
        bottom: 0;
        width: 1px;
        transform: scalex(0.5);
        background: #ddd;
      }
    }
  }
  @keyframes fadein {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
}
</style>

2. plugins/message/index.js

import { createvnode, render } from "vue";
import messageconstructor from "./index.vue";

const $message = function(options) {
  // 创建div
  const container = document.createelement("div");
  container.classname = `__default__container__message__`;
  //创建虚拟节点
  const vm = createvnode(messageconstructor, options);
  //渲染虚拟节点
  render(vm, container);
  document.body.appendchild(container);
};

export default {
  //组件注册
  install(app) {
    app.config.globalproperties.$message = $message;
  }
};

3. main.js

import { createapp } from "vue";
import router from "./router";
import store from "./store";
import app from "./app.vue";

import message from "./assets/plugins/message";

const app = createapp(app);

app.use(message);

app.use(store);
app.use(router);
app.mount("#app", { username: "123 });

4. 使用方法

import { getcurrentinstance } from "vue";
setup(props) {
	const { ctx } = getcurrentinstance();
	
	  ctx.$message({
        content: "您确定要退出吗?",
        cancletext: "取消",
        success: (res) => {
          	console.log(res);
        },
        fail: (err) => {
			console.log(err);
		},
      });
}

5. 升级改造

由于当前 ctx.$message 仅仅只在setup里面使用, 如果希望在unit.js或者其他js文件中使用? 下面开始我的表演

  • 5.1 js文件中需要使用vue的app实例对象
// main.js
export const app = createapp(app);
  • 5.2 js文件引入app实例
// util.js
import { app } from "../../main";

/**
 * @params title string
 * @params content string
 * @params oktext string
 * @params oktextcolor string
 * @params cancletext string
 * @params cancletextcolor string
 * @return promise
 */
export function showmessage(params) {
  return new promise((resolve, reject) => {
    app.config.globalproperties.$message({
      ...params,
      success: () => {
        return resolve("is:ok");
      },
      fail: () => {
        return reject("is:cancle");
      }
    });
  });
}
  • 5.3 使用方法
import { showmessage } from "../../assets/scripts/util";
setup(){
	 const logout = () => {
      showmessage({ content: "您确定要退出吗?", cancletext: "取消" })
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    };
   return { logout };
}

文档说明:

字段说明requiredtype默认
title标题falsestring提示
content内容truestring
oktext确定按钮文字falsestring确定
oktextcolor确定按钮文字颜色falsestring#26a2ff
cancletext取消按钮文字falsestring
cancletextcolor取消按钮文字颜色falsestring#999
success成功回调falsefunction
fail失败回调falsefunction

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com