十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
小編給大家分享一下vue組件化開發(fā)指的是什么意思,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開發(fā)技術(shù)和網(wǎng)頁設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)建站專業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站制作(企業(yè)站、自適應(yīng)網(wǎng)站建設(shè)、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!
在vue中,組件化開發(fā)指的是將復(fù)雜的業(yè)務(wù)拆分為多個(gè)組件,每個(gè)組件依賴的CSS、JS、模板、圖片等資源放在一起開發(fā)和維護(hù)。因?yàn)榻M件是資源獨(dú)立的,所以組件在系統(tǒng)內(nèi)部可復(fù)用,且可以極大簡化代碼量,對后期的需求變更和維護(hù)也更加友好。
本教程操作環(huán)境:windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。
什么叫做組件化
vue.js 有兩大法寶,一個(gè)是數(shù)據(jù)驅(qū)動(dòng),另一個(gè)就是組件化,那么問題來了,什么叫做組件化,為什么要組件化?接下來我就針對這兩個(gè)問題一一解答,所謂組件化,就是把頁面拆分成多個(gè)組件,每個(gè)組件依賴的 CSS、JS、模板、圖片等資源放在一起開發(fā)和維護(hù)。因?yàn)榻M件是資源獨(dú)立的,所以組件在系統(tǒng)內(nèi)部可復(fù)用,組件和組件之間可以嵌套,如果項(xiàng)目比較復(fù)雜,可以極大簡化代碼量,并且對后期的需求變更和維護(hù)也更加友好。
1、組件化開發(fā)指的是將復(fù)雜的業(yè)務(wù)拆分為一個(gè)又一個(gè)的組件
2、組件化開發(fā)的組件一般來說要靈活
3、組件化開發(fā)涉及到了Vue的js組件封裝,需要掌握Vue基礎(chǔ)、Vue實(shí)例方法與屬性、Vue.extend、Vue插件等知識
如何進(jìn)行組件化開發(fā)
先看下圖:
這是 vue.js 中的一個(gè)報(bào)錯(cuò),原因是使用了一個(gè)未經(jīng)注冊的組件 lx-xxx
,這個(gè)報(bào)錯(cuò)告訴我們一個(gè)道理:使用自定義組件之前必須注冊。
那么如何注冊一個(gè)組件呢? Vue.js 提供了 2 種組件的注冊方式,全局注冊和局部注冊。
在 vue.js 中我們可以使用 Vue.component(tagName, options) 進(jìn)行全局注冊,例如
Vue.component('my-component', { // 選項(xiàng) })
Vue.js 也同樣支持局部注冊,我們可以在一個(gè)組件內(nèi)部使用 components 選項(xiàng)做組件的局部注冊,例如:
import HelloWorld from './components/HelloWorld' export default { components: { HelloWorld } }
區(qū)別:全局組件是掛載在 Vue.options.components
下,而局部組件是掛載在 vm.$options.components
下,這也是全局注冊的組件能被任意使用的原因。
所謂工欲善其事,必先利其器,在正式開發(fā)一個(gè)組件之前,我們先要掌握一些必備的知識,這里我只會簡單介紹一下,詳情參考官網(wǎng)。
name
組件的名稱,必填
name: 'lxNiu'
js 中使用駝峰式命令,HTML 使用kebab-case命名。
props
組件屬性,用于父子組件通信,可通過this.msg訪問
{{msg}}props: { msg: { type: String, default: '' } } show: Boolean // 默認(rèn)false msg: [String, Boolean] // 多種類型
computed
處理data或者props中的屬性,并返回一個(gè)新屬性
{{newMsg}}computed: { newMsg() { return 'hello ' + this.msg } },
注:因?yàn)閜rops,data和computed在編譯階段都會作為vm的屬性合并,所以不可重名
render
用render函數(shù)描述template
hello world
render 中的 h 其實(shí)就是 createElement,它接受三個(gè)參數(shù),返回一個(gè) vnode
h 參數(shù)解釋:
args1: {string | Function | Object} 用于提供DOM的html內(nèi)容
args2: {Object} 設(shè)置DOM樣式、屬性、綁定事件之類
args3: {array} 用于設(shè)置分發(fā)的內(nèi)容
注:vue編譯順序: template–> compile --> render --> vnode --> patch --> DOM
slot
headerfooter
class
定義子組件的類名
// 父組件// 子組件 控制//真實(shí)DOMhello
style
向子組件傳遞樣式
// 父組件// 子組件 hello world
其他屬性
$attrs
v-bind="$attrs" 將除class和style外的屬性添加到父組件上,如定義input:
v-once
組件只渲染一次,后面即使數(shù)據(jù)發(fā)生變化也不會重新渲染,比如例子中val不會變成456
{{val}}
mixins
// mixin.js export default { data() { return{ msg: 'hello world' } }, methods: { clickBtn() { console.log(this.msg) } }, } // index.vue import actionMixin from "./mixin.js"; export default { methods: {}, mixins: [actionMixin] }
比如我們要注冊一個(gè) lx-button 這樣一個(gè)組件,那么目錄和偽代碼如下:
index.vue
index.js
import lxButton from './src/index' lxButton.install = (Vue) => { Vue.component(lxButton.name, lxButton) } export default lxButton
其中 install
是 Vue.js 提供了一個(gè)公開方法,這個(gè)方法的第一個(gè)參數(shù)是 Vue 構(gòu)造器,第二個(gè)參數(shù)是一個(gè)可選的選項(xiàng)對象。MyPlugin.install = function (Vue, options){}
參考: 開發(fā)插件
https://cn.vuejs.org/v2/guide/plugins.html#%E5%BC%80%E5%8F%91%E6%8F%92%E4%BB%B6
watch-彈窗實(shí)現(xiàn)原理
定義組件
點(diǎn)擊父組件中的顯示
按鈕,改變傳入子組件中的值,點(diǎn)擊子組件中的關(guān)閉
,改變父組件中值。
注:@click=“dialogVisible = true” 點(diǎn)擊時(shí)將dialogVisible的值改為true
注::visible.sync: 雙向數(shù)據(jù)綁定,配合update:visible使用,實(shí)現(xiàn)子組件修改父組件中的值
官網(wǎng)解釋: sync
col組件實(shí)例
export default { name: 'ElCol', props: { span: { type: Number, default: 24 }, tag: { type: String, default: 'div' }, offset: Number, pull: Number, push: Number, xs: [Number, Object], sm: [Number, Object], md: [Number, Object], lg: [Number, Object], xl: [Number, Object] }, computed: { gutter() { let parent = this.$parent; while (parent && parent.$options.componentName !== 'ElRow') { parent = parent.$parent; } return parent ? parent.gutter : 0; } }, render(h) { let classList = []; let style = {}; if (this.gutter) { style.paddingLeft = this.gutter / 2 + 'px'; style.paddingRight = style.paddingLeft; } ['span', 'offset', 'pull', 'push'].forEach(prop => { if (this[prop] || this[prop] === 0) { classList.push( prop !== 'span' ? `el-col-${prop}-${this[prop]}` : `el-col-${this[prop]}` ); } }); ['xs', 'sm', 'md', 'lg', 'xl'].forEach(size => { if (typeof this[size] === 'number') { classList.push(`el-col-${size}-${this[size]}`); } else if (typeof this[size] === 'object') { let props = this[size]; Object.keys(props).forEach(prop => { classList.push( prop !== 'span' ? `el-col-${size}-${prop}-${props[prop]}` : `el-col-${size}-${props[prop]}` ); }); } }); return h(this.tag, { class: ['el-col', classList], style }, this.$slots.default); } };
col組件使用render函數(shù),而不是template來實(shí)現(xiàn)組件,原因有兩個(gè):
該組件有大量的類判斷,如果采用template代碼比較冗余,使用js代碼更加簡潔
直接render描述性能更好
官網(wǎng)解釋: render-function
button組件實(shí)例
局部組件實(shí)例
看完了這篇文章,相信你對“vue組件化開發(fā)指的是什么意思”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!