今天小編給大家分享一下怎么使用Vue解決動(dòng)態(tài)掛載的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
專注于為中小企業(yè)提供網(wǎng)站制作、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)錫林郭勒盟免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
我們的電子表格控件SpreadJS在運(yùn)行時(shí),存在這樣一個(gè)功能:當(dāng)用戶雙擊單元格會(huì)顯示一個(gè)輸入框用于編輯單元格的內(nèi)容,用戶可以根據(jù)需求按照自定義單元格類型的規(guī)范自定義輸入框的形式,集成任何Form表單輸入類型。
這個(gè)輸入框的創(chuàng)建銷毀都是通過繼承單元格類型對應(yīng)方法實(shí)現(xiàn)的,因此這里就存在一個(gè)問題——這個(gè)動(dòng)態(tài)的創(chuàng)建方式并不能簡單在VUE template中配置,然后直接使用。
而就在前不久,客戶問然詢問我:你家控件的自定義單元格是否支持Vue組件比如ElementUI的AutoComplete?
由于前面提到的這個(gè)問題:
沉思許久,我認(rèn)真給客戶回復(fù):“組件運(yùn)行生命周期不一致,用不了”,但又話鋒一轉(zhuǎn),表示可以使用通用組件解決這個(gè)問題。
問題呢,是順利解決了。
但是這個(gè)無奈的"用不了",卻也成為我這幾天午夜夢回跨不去的坎。
后來,某天看Vue文檔時(shí),我想到App是運(yùn)行時(shí)掛載到#app上的。,從理論上來說,其他組件也應(yīng)該能動(dòng)態(tài)掛載到需要的Dom上,這樣創(chuàng)建時(shí)機(jī)的問題不就解決了嘛!
讓我們繼續(xù)查看文檔,全局APIVue.extend( options )是通過extend創(chuàng)建的。Vue實(shí)例可以使用$mount方法直接掛載到DOM元素上——這正是我們需要的。
<div id="mount-point"></div> // 創(chuàng)建構(gòu)造器 var Profile = Vue.extend({ template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>', data: function () { return { firstName: 'Walter', lastName: 'White', alias: 'Heisenberg' } } }) // 創(chuàng)建 Profile 實(shí)例,并掛載到一個(gè)元素上。 new Profile().$mount('#mount-point')
按照SpreadJS自定義單元格示例創(chuàng)建AutoCompleteCellType,并設(shè)置到單元格中:
function AutoComplateCellType() { } AutoComplateCellType.prototype = new GC.Spread.Sheets.CellTypes.Base(); AutoComplateCellType.prototype.createEditorElement = function (context, cellWrapperElement) { // cellWrapperElement.setAttribute("gcUIElement", "gcEditingInput"); cellWrapperElement.style.overflow = 'visible' let editorContext = document.createElement("div") editorContext.setAttribute("gcUIElement", "gcEditingInput"); let editor = document.createElement("div"); // 自定義單元格中editorContext作為容器,需要在創(chuàng)建一個(gè)child用于掛載,不能直接掛載到editorContext上 editorContext.appendChild(editor); return editorContext; } AutoComplateCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) { let width = cellRect.width > 180 ? cellRect.width : 180; if (editorContext) { // 創(chuàng)建構(gòu)造器 var Profile = Vue.extend({ template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>', data: function () { return { firstName: 'Walter', lastName: 'White', alias: 'Heisenberg' } } }) // 創(chuàng)建 Profile 實(shí)例,并掛載到一個(gè)元素上。 new Profile().$mount(editorContext.firstChild); } };
運(yùn)行,雙擊進(jìn)入編輯狀態(tài),結(jié)果卻發(fā)現(xiàn)報(bào)錯(cuò)了
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
根據(jù)報(bào)錯(cuò)提示,此時(shí)候我們有兩種解決辦法:
開啟runtimeCompiler,在vue.config.js中加入runtimeCompiler: true的配置,允許運(yùn)行時(shí)編譯,這樣可以動(dòng)態(tài)生成template,滿足動(dòng)態(tài)組件的需求
提前編譯模板僅動(dòng)態(tài)掛載,autocomplete的組件是確定的,我們可以使用這種方法
新建AutoComplete.vue組件用于動(dòng)態(tài)掛載,這樣可以掛載編譯好的組件。
<template> <div> <p>{{ firstName }} {{ lastName }} aka {{ alias }}</p> </div> </template> <script> export default { data: function () { return { firstName: "Walter", lastName: "White", alias: "Heisenberg", }; }, }; </script> import AutoComplate from './AutoComplate.vue' AutoComplateCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) { let width = cellRect.width > 180 ? cellRect.width : 180; if (editorContext) { // 創(chuàng)建構(gòu)造器 var Profile = Vue.extend(AutoComplate); // 創(chuàng)建 Profile 實(shí)例,并掛載到一個(gè)元素上。 new Profile().$mount(editorContext.firstChild); } };
雙擊進(jìn)入編輯狀態(tài),看到組件中的內(nèi)容
下一步,對于自定義單元格還需要設(shè)置和獲取組件中的編輯內(nèi)容,這時(shí)通過給組件添加props,同時(shí)在掛載時(shí)創(chuàng)建的VueComponent實(shí)例上直接獲取到所有props內(nèi)容,對應(yīng)操作即可實(shí)現(xiàn)數(shù)據(jù)獲取設(shè)置。
更新AutoComplate.vue,添加props,增加input用于編輯
<template> <div> <p>{{ firstName }} {{ lastName }} aka {{ alias }}</p> <input type="text" v-model="value"> </div> </template> <script> export default { props:["value"], data: function () { return { firstName: "Walter", lastName: "White", alias: "Heisenberg", }; }, }; </script>
通過this.vm存儲(chǔ)VueComponent實(shí)例,在getEditorValue 和setEditorValue 方法中獲取和給VUE組件設(shè)置Value。編輯結(jié)束,通過調(diào)用$destroy()方法銷毀動(dòng)態(tài)創(chuàng)建的組件。
AutoComplateCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) { let width = cellRect.width > 180 ? cellRect.width : 180; if (editorContext) { // 創(chuàng)建構(gòu)造器 var Profile = Vue.extend(MyInput); // 創(chuàng)建 Profile 實(shí)例,并掛載到一個(gè)元素上。 this.vm = new Profile().$mount(editorContext.firstChild); } }; AutoComplateCellType.prototype.getEditorValue = function (editorContext) { // 設(shè)置組件默認(rèn)值 if (this.vm) { return this.vm.value; } }; AutoComplateCellType.prototype.setEditorValue = function (editorContext, value) { // 獲取組件編輯后的值 if (editorContext) { this.vm.value = value; } }; AutoComplateCellType.prototype.deactivateEditor = function (editorContext, context) { // 銷毀組件 this.vm.$destroy(); this.vm = undefined; };
整個(gè)流程跑通了,下來只需要在AutoComplate.vue中,將input替換成ElementUI 的el- autocomplete并實(shí)現(xiàn)對應(yīng)方法就好了。
讓我們看看效果吧。
以上就是“怎么使用Vue解決動(dòng)態(tài)掛載”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:怎么使用Vue解決動(dòng)態(tài)掛載
地址分享:http://jinyejixie.com/article10/iejcdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、域名注冊、云服務(wù)器、面包屑導(dǎo)航、電子商務(wù)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)