vue2封装一个确认框组件

逛掘金社区的一个体验就是经常会打开很多页面。发个沸点也要打开新页面,一点不及时啊。

所以我使用油猴插件实现了在本页面直接打开确认框发沸点的功能。

封装一个确认框组件

js 复制代码
const ConfirmBox = {
  name: 'ConfirmBox',
  props: {
    title: {
      type: String,
      default: '提示',
    },
    content: String,
  },
  render(h) {
    const content = this.$slots.default || this.content;

    return h(
      'div',
      {
        staticClass: 'xx-confirm-box',
      },
      [
        h('div', { staticClass: 'xx-confirm-box-title' }, [
          h('div', { staticClass: 'title' }, this.title),
          h(
            'a',
            { staticClass: 'close', on: { click: this.handleClose } },
            '关闭'
          ),
        ]),
        h('div', { staticClass: 'xx-confirm-box-content' }, [content]),
        h('div', { staticClass: 'xx-confirm-box-footer' }, [
          h('button', { on: { click: this.handleClose } }, '取消'),
          h('button', { on: { click: this.handleConfirm } }, '确认'),
        ]),
      ]
    );
  },
  methods: {
    handleClose(e) {
      this.$emit('close', e);
    },
    handleConfirm(e) {
      this.$emit('confirm', e);
    },
  },
};
scss 复制代码
.xx-confirm-box {
  position: fixed;
  top: 80px;
  left: 0;
  right: 0;
  margin: auto;
  padding: 5px 10px;
  width: fit-content;
  background: #e8f3ff;
  z-index: 2999;
  color: var(--juejin-brand-1-normal);
  border-radius: 5px;
  white-space: pre-wrap;
  line-height: 1.8;
  border: solid 1px;
  
  &-title {
    display: flex;
    justify-content: space-between;
    padding-bottom: 4px;
    .title {
      flex: 1;
      font-weight: 600;
    }
  }
  &-footer {
    margin-top: 10px;
    display: flex;
    button {
      flex: 1;
      &:first-child {
        background-color: #7bbdff;
      }
    }
  }
}

便捷使用方式封装

js 复制代码
const ConfirmBoxConstructor = Vue.extend(ConfirmBox);

export const confirm = (options = {}) => {
  if (typeof options === 'string') options = { content: options };
  const { content, title, onConfirm, onClose } = options;
  let instance;
  const props = { content, title };
  const on = {};
  const close = () => instance.$destroy();
  on.close = onClose ? () => onClose(close) : close;
  on.confirm = onConfirm ? () => onConfirm(close) : close;

  if (typeof content === 'string') {
    instance = new ConfirmBoxConstructor({ propsData: props, on });
  } else {
    const CompConstructor = Vue.extend({
      render: h => h(ConfirmBox, { props, on }, [content]),
    });
    instance = new CompConstructor();
  }

  instance.$on('hook:mounted', () => {
    document.body.appendChild(instance.$el);
  });
  instance.$on('hook:destroyed', () => {
    instance.$el.remove();
  });

  instance.$mount();
};

使用方式

js 复制代码
const content = '每日打卡,从使用今日掘友分油猴插件开始。\nhttps://juejin.cn/post/7280006996572340283';

confirm({
  title: '输入沸点内容',
  content: h(
    'textarea',
    {
      attrs: { rows: '3' },
      style: { width: '300px' },
    },
    content
  ),
  onConfirm: close => {
    close();
    // 发接口
    publishShortMsg(content).then(msg => {
      message('发布一条沸点成功').then(() => {
        window.open(`/pin/${msg.msg_id}`);
      });
    });
  },
});
相关推荐
万少12 分钟前
Trae AI 编辑器6大使用规则
前端·javascript·人工智能
好玩的Matlab(NCEPU)34 分钟前
如何编写 Chrome 插件(Chrome Extension)
前端·chrome
Yan-英杰39 分钟前
Deepseek大模型结合Chrome搜索爬取2025AI投资趋势数据
前端·chrome
Crystal3281 小时前
app里video层级最高导致全屏视频上的操作的东西显示不出来的问题
前端·vue.js
weixin_445476681 小时前
Vue+redis全局添加水印解决方案
前端·vue.js·redis
lecepin1 小时前
AI Coding 资讯 2025-10-29
前端·后端·面试
余道各努力,千里自同风1 小时前
小程序中获取元素节点
前端·小程序
PineappleCoder1 小时前
大模型也栽跟头的 Promise 题!来挑战一下?
前端·面试·promise
非凡ghost1 小时前
MousePlus(鼠标增强工具) 中文绿色版
前端·windows·计算机外设·软件需求
Moonbit2 小时前
MoonBit Pearls Vol.13:初探 MoonBit 中的 JavaScript 交互
javascript·后端