# 如何编写 Vue render 组件?

# 为什么?

答:提供更强大的编程与流程控制能力,卸载组件重置状态、删除 DOM、移除事件监听...

# 主要结构

src
│  └─components
│       └─CustomModal
│             └─index.js
│             └─Render.vue
│             └─Template.vue
1
2
3
4
5
6

# index.js

import Vue from "vue";
import template from "./Template.vue";
const CustomModalConstructor = Vue.extend(template);
const CustomModal = async ({
  render,
  onOk = () => {},
  onCancel = () => {},
  title = "标题",
  localeCancelText = "取消",
  localeOkText = "确定",
  showHead = true,
  footerHide = false
}) => {
  let vm = new CustomModalConstructor({
    created() {
      this.title = title;
      this.localeCancelText = localeCancelText;
      this.localeOkText = localeOkText;
      this.showHead = showHead;
      this.footerHide = footerHide;
      this.render = render;
      this.onOk = onOk;
      this.onCancel = onCancel;
    }
  });
  vm.$mount();
  document.body.appendChild(vm.$el);
  return vm;
};
export default CustomModal;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# Render.vue

<script>
  export default {
    name: "Render",
    props: {
      render: {
        type: Function,
        default: () => {}
      }
    },
    render() {
      return this.render();
    }
  };
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Template.vue

<template>
  <div v-show="visible">
    <transition name="fade" mode="out-in">
      <div class="mongolian-layer">
        <div class="layer">
          <div v-if="showHead" class="title">
            <div class="title-text">{{ title }}</div>
            <a class="title-close" @click="cancel">
              <slot name="close">
                <Icon type="ios-close"></Icon>
              </slot>
            </a>
          </div>
          <div class="body">
            <Render :render="render" />
          </div>
          <div v-if="!footerHide" class="footer">
            <i-button type="text" size="large" @click.native="cancel"
              >{{ localeCancelText }}</i-button
            >
            <i-button type="primary" size="large" @click.native="ok"
              >{{ localeOkText }}</i-button
            >
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
  import Render from "./Render";
  export default {
    components: {
      Render
    },
    props: {
      title: String,
      localeCancelText: String,
      localeOkText: String,
      showHead: {
        type: Boolean,
        default: true
      },
      footerHide: {
        type: Boolean,
        default: false
      },
      render: Function,
      onOk: Function,
      onCancel: Function
    },
    data() {
      return {
        visible: true
      };
    },
    methods: {
      /**
       * @description: 关闭弹窗移除dom、解绑事件
       * @param {type}
       * @return:
       */
      destroyed() {
        this.visible = false;
        this.$el.remove();
      },
      ok() {
        this.onOk();
        this.destroyed();
      },
      cancel() {
        this.onCancel();
        this.destroyed();
      }
    }
  };
</script>
<style lang="less" scoped>
  .mongolian-layer {
    position: fixed;
    overflow: auto;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1000;
    background-color: rgba(55, 55, 55, 0.6);
    height: 100%;
    -webkit-overflow-scrolling: touch;
    outline: 0;
    text-align: center;
    .layer {
      min-width: 520px;
      display: inline-block;
      margin: 0 auto;
      position: relative;
      outline: 0;
      top: 100px;
      background-color: @WHITE;
      text-align: left;
      border: 0;
      border-radius: 6px;
      background-clip: padding-box;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      .title {
        border-bottom: 1px solid #e8eaec;
        padding: 14px 16px;
        line-height: 1;
        .title-text {
          display: inline-block;
          width: 100%;
          height: 20px;
          line-height: 20px;
          font-size: 14px;
          color: #17233d;
          font-weight: 700;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .title-close {
          z-index: 1;
          font-size: 12px;
          position: absolute;
          right: 8px;
          top: 8px;
          overflow: hidden;
          cursor: pointer;
          .ivu-icon-ios-close {
            font-size: 31px;
            color: #999;
            transition: color 0.2s ease;
            position: relative;
            top: 1px;
          }
        }
      }
      .body {
        padding: 16px;
        font-size: 12px;
        line-height: 1.5;
      }
      .footer {
        border-top: 1px solid #e8eaec;
        padding: 12px 18px;
        text-align: right;
      }
    }
  }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

# 怎么使用?

// main.js
import CustomModal from "@/components/CustomModal";
Vue.prototype.$CustomModal = CustomModal;
1
2
3
this.$CustomModal({
  render: () => {
    return <div>123</div>;
  },
  onOk: () => {
    console.log("ok");
  },
  onCancel: () => {
    console.log("cancel");
  }
}); // 传入参数就完事了
1
2
3
4
5
6
7
8
9
10
11