摘要:插件分類插件通常會(huì)為添加全局功能,插件的編寫方法一般分為類,如上圖所示的插件應(yīng)當(dāng)有一個(gè)公開(kāi)方法。
前言
前段時(shí)間看到黃軼老師的一篇文章感觸頗多。特別是下面這一段話
插件 Vue 化引發(fā)的一些思考
這篇文章我不僅僅是要教會(huì)大家封裝一個(gè) scroll 組件,還想傳遞一些把第三方插件(原生 JS 實(shí)現(xiàn))Vue 化的思考過(guò)程。很多學(xué)習(xí) Vue.js 的同學(xué)可能還停留在 “XX 效果如何用 Vue.js 實(shí)現(xiàn)” 的程度,其實(shí)把插件 Vue 化有兩點(diǎn)很關(guān)鍵,一個(gè)是對(duì)插件本身的實(shí)現(xiàn)原理很了解,另一個(gè)是對(duì) Vue.js 的特性很了解。對(duì)插件本身的實(shí)現(xiàn)原理了解需要的是一個(gè)思考和鉆研的過(guò)程,這個(gè)過(guò)程可能困難,但是收獲也是巨大的;而對(duì) Vue.js 的特性的了解,是需要大家對(duì) Vue.js 多多使用,學(xué)會(huì)從平時(shí)的項(xiàng)目中積累和總結(jié),也要善于查閱 Vue.js 的官方文檔,關(guān)注一些 Vue.js 的升級(jí)等。
所以,我們拒絕伸手黨,但也不是鼓勵(lì)大家什么時(shí)候都要去造輪子,當(dāng)我們?cè)谑褂靡恍┈F(xiàn)成插件的同時(shí),也希望大家能多多思考,去探索一下現(xiàn)象背后的本質(zhì),把 “XX 效果如何用 Vue.js 實(shí)現(xiàn)” 這句話從問(wèn)號(hào)變成句號(hào)。
插件分類插件通常會(huì)為 Vue 添加全局功能,插件的編寫方法一般分為4類,如上圖所示
Vue.js 的插件應(yīng)當(dāng)有一個(gè)公開(kāi)方法 install 。這個(gè)方法的第一個(gè)參數(shù)是 Vue 構(gòu)造器,第二個(gè)參數(shù)是一個(gè)可選的選項(xiàng)對(duì)象
MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或?qū)傩? Vue.myGlobalMethod = function () { // 邏輯... } // 2. 添加全局資源 Vue.directive("my-directive", { bind (el, binding, vnode, oldVnode) { // 邏輯... } ... }) // 3. 注入組件 Vue.mixin({ created: function () { // 邏輯... } ... }) // 4. 添加實(shí)例方法 Vue.prototype.$myMethod = function (methodOptions) { // 邏輯... } }插件編寫方法 1. 添加全局方法或?qū)傩?/b>
export default { install(Vue, option) { Vue.$myName = "羅輯", Vue.$myJob = "面壁者", Vue.$do = () => { // 全局方法 } } }
在install方法中,我們直接在Vue實(shí)例上聲明了$myName屬性并進(jìn)行了賦值,當(dāng)該插件注冊(cè)后只要存在Vue實(shí)例的地方你都可以獲取到Vue.$myName的值,因?yàn)槠渲苯咏壎ㄔ诹薞ue實(shí)例上。
2. 添加全局資源export default { install(Vue, options) { Vue.directive("dom", { bind: function() {}, // 當(dāng)綁定元素插入到 DOM 中。 inserted: function(el, binding, vnode, oldVnode) { // 移動(dòng)元素 el.tranfromDom(); }, update: function() {}, componentUpdated: function() {}, unbind: function() {} }); }, }
添加全局資源包含了添加全局的指令/過(guò)濾器/過(guò)渡等,上方代碼我們通過(guò)Vue.directive()添加了一個(gè)全局指令v-dom,其主要包含了5種方法,其中inserted代表當(dāng)綁定元素插入到 DOM 中執(zhí)行,而el.tranfromDom()代表要移動(dòng)的元素,這樣如果我們?cè)谝粋€(gè)Modal彈窗上綁定該指令就會(huì)自動(dòng)的移動(dòng)dom(實(shí)現(xiàn)實(shí)際dom位子與模板中dom位子相分離)。
// Thanks to: https://github.com/airyland/vux/blob/v2/src/directives/transfer-dom/index.js // Thanks to: https://github.com/calebroseland/vue-dom-portal /** * Get target DOM Node * @param {(Node|string|Boolean)} [node=document.body] DOM Node, CSS selector, or Boolean * @return {Node} The target that the el will be appended to */ function getTarget (node) { if (node === void 0) { node = document.body } if (node === true) { return document.body } return node instanceof window.Node ? node : document.querySelector(node) } const directive = { inserted (el, { value }, vnode) { if (el.dataset.transfer !== "true") return false; el.className = el.className ? el.className + " v-transfer-dom" : "v-transfer-dom"; const parentNode = el.parentNode; if (!parentNode) return; const home = document.createComment(""); let hasMovedOut = false; if (value !== false) { parentNode.replaceChild(home, el); // moving out, el is no longer in the document getTarget(value).appendChild(el); // moving into new place hasMovedOut = true } if (!el.__transferDomData) { el.__transferDomData = { parentNode: parentNode, home: home, target: getTarget(value), hasMovedOut: hasMovedOut } } }, componentUpdated (el, { value }) { if (el.dataset.transfer !== "true") return false; // need to make sure children are done updating (vs. `update`) const ref$1 = el.__transferDomData; if (!ref$1) return; // homes.get(el) const parentNode = ref$1.parentNode; const home = ref$1.home; const hasMovedOut = ref$1.hasMovedOut; // recall where home is if (!hasMovedOut && value) { // remove from document and leave placeholder parentNode.replaceChild(home, el); // append to target getTarget(value).appendChild(el); el.__transferDomData = Object.assign({}, el.__transferDomData, { hasMovedOut: true, target: getTarget(value) }); } else if (hasMovedOut && value === false) { // previously moved, coming back home parentNode.replaceChild(el, home); el.__transferDomData = Object.assign({}, el.__transferDomData, { hasMovedOut: false, target: getTarget(value) }); } else if (value) { // already moved, going somewhere else getTarget(value).appendChild(el); } }, unbind (el) { if (el.dataset.transfer !== "true") return false; el.className = el.className.replace("v-transfer-dom", ""); const ref$1 = el.__transferDomData; if (!ref$1) return; if (el.__transferDomData.hasMovedOut === true) { el.__transferDomData.parentNode && el.__transferDomData.parentNode.appendChild(el) } el.__transferDomData = null } }; export default directive;
ivew 中的v-transfer-dom指令
3. 注入組件 添加全局Mixinexport default { install(Vue, options) { Vue.mixin({ methods: { say() { console.log("hello.."); } } }); }, }
mixin代表混合的意思,我們可以全局注冊(cè)一個(gè)Mixin,其會(huì)影響注冊(cè)之后創(chuàng)建的每個(gè) Vue 實(shí)例,上方代碼注冊(cè)后會(huì)在每個(gè)組件實(shí)例中添加say方法,在單文件組件中可以直接通過(guò)this.say()調(diào)用。當(dāng)然如果實(shí)例中存在同名方法,則mixin方法中創(chuàng)建的會(huì)被覆蓋,同時(shí)mixin對(duì)象中的鉤子將在組件自身鉤子之前調(diào)用。
/** * Show migrating guide in browser console. * * Usage: * import Migrating from "element-ui/src/mixins/migrating"; * * mixins: [Migrating] * * add getMigratingConfig method for your component. * getMigratingConfig() { * return { * props: { * "allow-no-selection": "allow-no-selection is removed.", * "selection-mode": "selection-mode is removed." * }, * events: { * selectionchange: "selectionchange is renamed to selection-change." * } * }; * }, */ export default { mounted() { if (process.env.NODE_ENV === "production") return; if (!this.$vnode) return; const { props, events } = this.getMigratingConfig(); const { data, componentOptions } = this.$vnode; const definedProps = data.attrs || {}; const definedEvents = componentOptions.listeners || {}; for (let propName in definedProps) { if (definedProps.hasOwnProperty(propName) && props[propName]) { console.warn(`[Element Migrating][Attribute]: ${props[propName]}`); } } for (let eventName in definedEvents) { if (definedEvents.hasOwnProperty(eventName) && events[eventName]) { console.warn(`[Element Migrating][Event]: ${events[eventName]}`); } } }, methods: { getMigratingConfig() { return { props: {}, events: {} }; } } };
element 的遷移引導(dǎo)mixin
function broadcast(componentName, eventName, params) { this.$children.forEach(child => { var name = child.$options.componentName; if (name === componentName) { child.$emit.apply(child, [eventName].concat(params)); } else { broadcast.apply(child, [componentName, eventName].concat([params])); } }); } export default { methods: { dispatch(componentName, eventName, params) { var parent = this.$parent || this.$root; var name = parent.$options.componentName; while (parent && (!name || name !== componentName)) { parent = parent.$parent; if (parent) { name = parent.$options.componentName; } } if (parent) { parent.$emit.apply(parent, [eventName].concat(params)); } }, broadcast(componentName, eventName, params) { broadcast.call(this, componentName, eventName, params); } } };
element 為Vue2.x添加簡(jiǎn)化版的 dispatch,broadcast(改方法vue1中有原生實(shí)現(xiàn))當(dāng)然這里還為做成多帶帶插件
4. 添加實(shí)例方法export default { install(Vue, option) { Vue.prototype.$myName = "羅輯"; Vue.prototype.showMyName = value => { console.log(value); }; } }
添加實(shí)例方法是最常用的一種方法,其直接綁定在vue的原型鏈上(一直是js的傳統(tǒng))。實(shí)例方法可以在組件內(nèi)部,通過(guò)this.$myMethod來(lái)調(diào)用。
使用插件過(guò)全局方法 Vue.use() 使用插件:
// 調(diào)用 `MyPlugin.install(Vue)` Vue.use(MyPlugin)
或者傳入一個(gè)選項(xiàng)對(duì)象:
Vue.use(MyPlugin, { someOption: true })
Vue.use 會(huì)自動(dòng)阻止注冊(cè)相同插件多次,屆時(shí)只會(huì)注冊(cè)一次該插件。
Vue.js 官方提供的一些插件 (例如 vue-router) 在檢測(cè)到 Vue 是可訪問(wèn)的全局變量時(shí)會(huì)自動(dòng)調(diào)用 Vue.use()。然而在例如 CommonJS 的模塊環(huán)境中,你應(yīng)該始終顯式地調(diào)用 Vue.use():
// 用 Browserify 或 webpack 提供的 CommonJS 模塊環(huán)境時(shí) var Vue = require("vue") var VueRouter = require("vue-router") // 不要忘了調(diào)用此方法 Vue.use(VueRouter)
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/89087.html
摘要:慢慢地,關(guān)于的原創(chuàng)學(xué)習(xí)文章已經(jīng)寫了多篇了會(huì)一直放出來(lái),目前篇,因此做一個(gè)合集,獻(xiàn)給那些對(duì)新版本腳手架使用和背后設(shè)計(jì)感興趣的同學(xué),都是一步一步去看源碼,也給官方提了幾次,合進(jìn)去了幾個(gè)原創(chuàng)不易,歡迎大家互相轉(zhuǎn)發(fā),期望大家一起快速過(guò)度到版本目錄 慢慢地,關(guān)于 Vue CLI 3 的原創(chuàng)學(xué)習(xí)文章已經(jīng)寫了 20 多篇了(會(huì)一直放出來(lái),目前 23 篇), 因此做一個(gè)合集,獻(xiàn)給那些對(duì)新版本腳手架使用...
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:而掃描各個(gè)模塊并合并路由表的腳本非常簡(jiǎn)單,讀寫文件就了。編寫插件之前先要理解抽象語(yǔ)法樹(shù)這個(gè)概念。的解析器,的配置。編寫腳本識(shí)別字段思路首先獲取到源代碼是類單文件的語(yǔ)法。獲取內(nèi)的字段,并替換成已生成的路由表。 話不多說(shuō)先上圖,簡(jiǎn)要說(shuō)明一下干了些什么事。圖可能太模糊,可以點(diǎn)svg看看showImg(https://segmentfault.com/img/bV3fs4?w=771&h=63...
閱讀 1155·2021-11-22 14:56
閱讀 1616·2019-08-30 15:55
閱讀 3448·2019-08-30 15:45
閱讀 1712·2019-08-30 13:03
閱讀 2936·2019-08-29 18:47
閱讀 3403·2019-08-29 11:09
閱讀 2716·2019-08-26 18:36
閱讀 2665·2019-08-26 13:55