|
1 | 1 | # 组件介绍 |
2 | 2 |
|
| 3 | +## 什么是组件?? |
| 4 | + |
| 5 | +通常认为页面上能看到所有内容都可以认为是组件。在开发上可以认为是UI和逻辑的一个整体。比较具体一点可以从`.vue`单文件结构来看: |
| 6 | +```vue |
| 7 | +<template> |
| 8 | + <!-- html --> |
| 9 | +</template> |
| 10 | +<style> |
| 11 | + /** css **/ |
| 12 | +</style> |
| 13 | +<script> |
| 14 | + // js |
| 15 | +</script> |
| 16 | +``` |
| 17 | +除此之外,还应具体生命周期。可以认为是页面的缩影。 |
| 18 | + |
| 19 | +## Vue 组件 |
| 20 | + |
3 | 21 | 从概念和使用方向来划分组件,一般可以分为下面三类: |
4 | 22 |
|
5 | 23 | ### 1. 页面级别的组件 |
|
14 | 32 |
|
15 | 33 |
|
16 | 34 | ##### 原生组件和Vue组件 |
| 35 | +```js |
| 36 | +import "./Modal.less" |
| 37 | +;(function(window) { |
| 38 | + function extendDefaults(source, properties) { |
| 39 | + var property |
| 40 | + for (property in properties) { |
| 41 | + if (properties.hasOwnProperty(property)) { |
| 42 | + source[property] = properties[property] |
| 43 | + } |
| 44 | + } |
| 45 | + return source |
| 46 | + } |
| 47 | + |
| 48 | + function getTransitionEnd() { |
| 49 | + var el = document.createElement("div") |
| 50 | + if (el.style.WebkitTransition) { |
| 51 | + return "webkitTransitionEnd" |
| 52 | + } |
| 53 | + return "transitionEnd" |
| 54 | + } |
| 55 | + |
| 56 | + function buildOut() { |
| 57 | + var content, contetnHolder, docFrag |
| 58 | + if (typeof this.options.content === "string") { |
| 59 | + content = this.options.content |
| 60 | + } else { |
| 61 | + content = this.options.content.innerHTML |
| 62 | + } |
| 63 | + |
| 64 | + docFrag = document.createDocumentFragment() |
| 65 | + |
| 66 | + this.modal = document.createElement("div") |
| 67 | + this.modal.className = "modal " + this.options.className |
| 68 | + this.modal.style.minWidth = this.options.minWidth + "px" |
| 69 | + this.modal.style.minHeight = this.options.minHeight + "px" |
| 70 | + |
| 71 | + if (this.options.closeButton) { |
| 72 | + this.closeButton = document.createElement("button") |
| 73 | + this.closeButton.className = "modal-close close-button" |
| 74 | + this.closeButton.innerHTML = "x" |
| 75 | + this.modal.appendChild(this.closeButton) |
| 76 | + } |
| 77 | + |
| 78 | + if (this.options.overlay) { |
| 79 | + this.overlay = document.createElement("div") |
| 80 | + this.overlay.className = "modal-overlay " + this.options.className |
| 81 | + docFrag.appendChild(this.overlay) |
| 82 | + } |
| 83 | + |
| 84 | + contetnHolder = document.createElement("div") |
| 85 | + contetnHolder.className = "modal-content" |
| 86 | + contetnHolder.innerHTML = content |
| 87 | + this.modal.appendChild(contetnHolder) |
| 88 | + |
| 89 | + docFrag.appendChild(this.modal) |
| 90 | + |
| 91 | + document.body.appendChild(docFrag) |
| 92 | + } |
| 93 | + |
| 94 | + function initializeEvents() { |
| 95 | + if (this.closeButton) { |
| 96 | + this.closeButton.addEventListener("click", this.close.bind(this)) |
| 97 | + } |
| 98 | + |
| 99 | + if (this.overlay) { |
| 100 | + this.overlay.addEventListener("click", this.close.bind(this)) |
| 101 | + } |
| 102 | + } |
| 103 | + window.Modal = function() { |
| 104 | + var defaults = { |
| 105 | + content: "", |
| 106 | + overlay: true, |
| 107 | + minWidth: 560, |
| 108 | + minHeight: 280, |
| 109 | + closeButton: true, |
| 110 | + className: "fade-and-drop" |
| 111 | + } |
| 112 | + |
| 113 | + this.closeButton = null |
| 114 | + this.modal = null |
| 115 | + this.overlay = null |
| 116 | + if (arguments[0] && typeof arguments[0] === "object") { |
| 117 | + this.options = extendDefaults(defaults, arguments[0]) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + window.Modal.prototype.open = function() { |
| 122 | + buildOut.call(this) |
| 123 | + initializeEvents.call(this) |
| 124 | + |
| 125 | + window.getComputedStyle(this.modal) |
| 126 | + |
| 127 | + this.modal.className = |
| 128 | + this.modal.className + |
| 129 | + (this.modal.offsetHeight > window.innerHeight |
| 130 | + ? " modal-open modal-anchored" |
| 131 | + : " modal-open") |
| 132 | + this.overlay.className = this.overlay.className + " modal-open" |
| 133 | + } |
| 134 | + |
| 135 | + window.Modal.prototype.close = function() { |
| 136 | + var $this = this |
| 137 | + this.modal.className = this.modal.className.replace(" modal-open", "") |
| 138 | + this.overlay.className = this.overlay.className.replace(" modal-open", "") |
| 139 | + |
| 140 | + this.modal.addEventListener("webkitTransitionEnd", function() { |
| 141 | + $this.modal.parentNode.removeChild($this.modal) |
| 142 | + }) |
| 143 | + |
| 144 | + this.overlay.addEventListener("webkitTransitionEnd", function() { |
| 145 | + $this.overlay.parentNode.removeChild($this.overlay) |
| 146 | + }) |
| 147 | + } |
| 148 | +})(window) |
| 149 | +``` |
| 150 | + |
| 151 | +```vue |
| 152 | +<template> |
| 153 | + <div> |
| 154 | + <transition name="fade-overlay"> |
| 155 | + <div class="lai-modal-overlay" v-if="overlay && isShow"></div> |
| 156 | + </transition> |
| 157 | + <transition name="fade-modal"> |
| 158 | + <div |
| 159 | + class="lai-modal" |
| 160 | + v-if="isShow" |
| 161 | + v-click-outside="hide" |
| 162 | + style="min-width: 560px; min-height: 280px;" |
| 163 | + > |
| 164 | + <button class="modal-close close-button" @click="hide">x</button> |
| 165 | + <div class="modal-content"> |
| 166 | + <slot></slot> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </transition> |
| 170 | + </div> |
| 171 | +</template> |
| 172 | +<script> |
| 173 | +import clickOutside from "../../directives/click-outside"; |
| 174 | +export default { |
| 175 | + props: { |
| 176 | + overlay: { |
| 177 | + type: Boolean, |
| 178 | + default: true |
| 179 | + }, |
| 180 | + value: { |
| 181 | + type: Boolean, |
| 182 | + default: false |
| 183 | + }, |
| 184 | + styles: { |
| 185 | + type: Object, |
| 186 | + default: () => {} |
| 187 | + } |
| 188 | + }, |
| 189 | + data() { |
| 190 | + return { |
| 191 | + isShow: this.value |
| 192 | + }; |
| 193 | + }, |
| 194 | + directives: { |
| 195 | + "click-outside": clickOutside |
| 196 | + }, |
| 197 | + methods: { |
| 198 | + hide() { |
| 199 | + this.isShow = false; |
| 200 | + this.$emit("input", this.isShow); |
| 201 | + } |
| 202 | + }, |
| 203 | + watch: { |
| 204 | + value(val) { |
| 205 | + if (this.ishow !== val) { |
| 206 | + this.isShow = val; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +}; |
| 211 | +</script> |
| 212 | +<style lang="less" scoped> |
| 213 | +... |
| 214 | +</style> |
| 215 | +``` |
17 | 216 |
|
18 | 217 | <box> |
19 | 218 | <global-modal-demo></global-modal-demo> |
20 | | -</box> |
| 219 | +</box> |
| 220 | + |
| 221 | +##### 一个modal的组件的开发: |
| 222 | + |
| 223 | +1. 原生 |
| 224 | + |
| 225 | +* 初始化 options |
| 226 | +* 创建生产 dom 和移除 dom 的方法 |
| 227 | +* 通过直接调用原生的 dom 操作方式来控制 modal 的显示和隐藏 |
| 228 | + |
| 229 | +2. vue |
| 230 | +* 初始化 `props` |
| 231 | +* 直接通过 vue render template 内容生产 `dom` |
| 232 | +* 通过 `vue` 自发的监听变量,来渲染dom (通过集成的 `virtural dom` 间接调用原生的 `dom` 操作方法) |
| 233 | + |
| 234 | + |
| 235 | + |
0 commit comments