发布于 1970-01-01 08:00
  • 2 个回答
    • 不知道你是怎么做的,我是这么做的

      1.首先你的那个innerHTML里面的东西我放在一个单独的.vue文件里;
      2.然后在js文件里面import进来,通过Vue.extend和new创建一个实例;
      3.在append后,在Vue.nextTick回调里完成isShow的改变

      我的Alert和Comfirm组件部分代码:
      Box.vue:

      <template>
          <transition name="fade">
              <p class="ui-alert" v-if="visible">
                  <p class="ui-msg-box">
                      <p class="ui-title" v-text="title">提示</p>
                      <p class="ui-msg" v-text="message"></p>
                      <p class="ui-buttons">
                          <a class="btn" href="javascript:;" @click="onOk">确认</a>
                          <a v-if="isConfirm" class="btn" href="javascript:;" @click="onCancel">取消</a>
                      </p>
                  </p>
                  <p class="ui-mask"></p>
              </p>
          </transition>
      </template>
      <script type="text/javascript">
          export default {
              props: {
                  title: String,
                  message : String
              },
              data() {
                  return {
                      isConfirm:true,
                      visible: false,
                      onOk:null,
                      onCancel:null
                  };
              },
              methods: {
                  onOk() {
                      this.onOk && this.onOk();
                  },
                  onCancel() {
                      this.onCancel && this.onCancel();
                  }
              }
          }
      </script>

      Box.js:

      import Vue from "vue";
      
      const AlertVue = Vue.extend(require('./box.vue'));
      
      
      let Message = (options = {}) => {
          
          let title = options.title || "提示",
              isConfirm = (options.isConfirm === undefined || options.isConfirm===false) ? false : true,
              message = typeof options === 'string' ? options : options.message;
              
          return new Promise((resolve, reject) => {
              let ins = new AlertVue({
                  el : document.createElement("p")
              });
              
              ins.message = message;
              ins.title = title;
              ins.isConfirm=isConfirm;
              ins.onOk = () => {
                  ins.visible = false;
                  resolve(true);
              }
              ins.onCancel = () => {
                  ins.visible = false;
                  resolve(false);
              }
              document.body.appendChild(ins.$el);
          
              Vue.nextTick(() => {
                    ins.visible = true;
              });
          });
      }
      
      let alert = (options = {}) => {
          let title = options.title || "提示";
          let message = typeof options === 'string' ? options : options.message;
          
          return new Message(Object.assign({
              isConfirm:false
          }, {
              title,
              message
          }));
      }
      
      let confirm = (options = {}) => {
          let title = options.title || "提示";
          let message = typeof options === 'string' ? options : options.message;
              
          return new Message(Object.assign({
              isConfirm:true
          }, {
              title,
              message
          }));
      }
      export default alert;
      export {confirm};
      2022-11-15 18:00 回答
    • 创建 DOM 元素之后再创建 Vue 实例,指定 el 包含生成的元素。

      2022-11-15 18:00 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有