vue2.0 自定義組件的方法(vue組件的封裝)
一、前言
之前的博客聊過 vue2.0和react的技術(shù)選型;聊過vue的axios封裝和vuex使用。今天簡(jiǎn)單聊聊 vue 組件的封裝。
vue 的ui框架現(xiàn)在是很多的,但是鑒于移動(dòng)設(shè)備的復(fù)雜性,兼容性問題突出。像 Mint-UI 等說實(shí)話很不錯(cuò)了,但是坑也是不少,而且很多功能也是僅憑這些實(shí)現(xiàn)不了,這需要我們?nèi)シ庋b自己的可復(fù)用組件
二、封裝組件的步驟
1. 建立組件的模板,先把架子搭起來,寫寫樣式,考慮你的組件的基本邏輯。
os:思考1小時(shí),碼碼10分鐘,程序猿的準(zhǔn)則。
2. 準(zhǔn)備組件的好數(shù)據(jù)輸入。即分析好邏輯,定好 props 里面的數(shù)據(jù)、類型。(后面詳解)
3.準(zhǔn)備組件的好數(shù)據(jù)輸出。即根據(jù)組件邏輯,做好要暴露出來的方法。(后面詳解)
4.封裝完畢了,直接調(diào)用即可。
os: 代碼可以不看,結(jié)論在文章最后
接下來以一個(gè)很簡(jiǎn)單的例子具體說明一下
現(xiàn)在先看一下demo的效果圖

三、 demo代碼
父組件:
<template>
<section class="f-mainPage">
<!--selectFunc 選擇完成的回調(diào) searchList 下拉列表的數(shù)據(jù)-->
<search @selectFunc="selectFunc" :searchList="searchList" :selectValue="selectValue"></search>
</section>
</template>
<script type="text/ecmascript-6">
import Search from '../vuePlugin/search'
export default {
data() {
return {
searchList: ['草船借箭', '大富翁', '測(cè)試數(shù)據(jù)'],
// 直接通過props傳遞對(duì)象 修改,挺便捷的,但是不規(guī)范
selectValue: {
data: '1'
},
// 通過emit修改,規(guī)范寫法
selectValue2: ''
}
},
mounted() {},
methods: {
pageGo(path) {
this.$router.push('/' + path)
},
selectFunc(value) {
this.selectValue2 = value
console.log(this.selectValue)
console.log(this.selectValue2)
}
},
components: {
Search
}
}
</script>
<style lang="scss" scoped>
.f-mainPage{
width: 100%;
.g-banner{
width: 100%;
background-image: url(../../../static/main_bg.png);
background-repeat: no-repeat;
background-size: 100% 100%;
position: relative;
overflow: hidden;
color: white;
text-align: center;
p:nth-child(1) {
margin: 10px auto 0px auto;
font-size: 1.3rem;
}
.f-banscri {
margin: 15px auto 8px auto;
font-size: 0.95rem;
}
.f-moneyMax{
margin: 5px auto 0px auto;
font-size: 2.4rem;
}
.f-returnCash{
width: 120px;
height: 35px;
text-align: center;
line-height: 35px;
background-color: white;
color: #169BD5;
display: inline-block;
border-radius: 5px;
font-size: 1rem;
margin-top: 35px;
position: relative;
.f-mmmbd{
position: absolute;
width: 100%;
height: 100%;
background-color: transparent;
top: 0;
left: 0;
}
}
}
.g-cashInfor{
width: 100%;
text-align: center;
display: flex;
justify-content: space-between;
div{
width: 50%;
height: 60px;
line-height: 60px;
box-sizing: border-box;
}
div:nth-child(1){
border-bottom: 1px solid #878787;
border-right: 1px solid #878787;
}
div:nth-child(2){
border-bottom: 1px solid #878787;
}
}
.g-operate{
width: 100%;
height: auto;
overflow: hidden;
ul{
list-style: none;
padding: 0;
margin: 0;
font-size: 1.05rem;
li{
height: 60px;
line-height: 60px;
padding-left: 25px;
position: relative;
span{
width: 20px;
height: 20px;
position: absolute;
top: 20px;
right: 20px;
background-image: url(../../../static/go.png);
background-repeat: no-repeat;
background-size: 100% 100%;
}
}
}
.f-goodNews{
width: 340px;
height: 144.5px;
margin: 20px auto 30px auto;
text-align: center;
background-image: url(../../../static/banner.png);
background-repeat: no-repeat;
background-size: 100% 100%;
}
}
}
</style>
子組件:
<template>
<div class="searchZJ">
<div class="f-search">
<div class="f-searchIn" v-bind:class="{searchInFous: this.fousFlag}">{{this.searchValue}}<span v-bind:class="{searchActive: this.searchFlag}" v-on:click="searchDown"></span></div>
<div class="f-searchXl" v-if="this.dataHas" v-bind:style="{height:this.searchFous, border:this.searchBorder}">
<div v-for="item in searchList" v-on:click="choseValue(item)">{{item}}</div>
</div>
<div class="f-searchXl" v-else >
<div>暫無數(shù)據(jù)</div>
</div>
</div>
</div>
</template>
<script type="text/ecmascript-6">
export default {
data() {
return {
data: [],
dataHas: true,
searchFlag: false,
searchFous: '0',
fousFlag: false,
searchValue: '',
searchBorder: 'none'
}
},
props: {
searchList: Array,
selectValue: Object
},
mounted() {
this.data = this.searchList
},
methods: {
searchDown() {
this.searchFlag === false ? this.searchFlag = true : this.searchFlag = false
this.searchFous === '0' ? this.searchFous = 'auto' : this.searchFous = '0'
this.searchBorder === 'none' ? this.searchBorder = '1px solid #D9D9D9' : this.searchBorder = 'none'
this.fousFlag === false ? this.fousFlag = true : this.fousFlag = false
},
choseValue(value) {
this.searchValue = value
this.searchDown()
this.selectValue.data = '我被修改了'
this.$emit('selectFunc', value)
}
}
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
.f-search{
width: 250px;
height: auto;
position: relative;
margin-left: 20px;
box-sizing: border-box;
}
.f-searchIn{
width: 250px;
height: 35px;
line-height: 35px;
font-size: 0.95rem;
border-radius: 5px;
overflow: hidden;
position: relative;
background-color: white;
box-shadow: none;
box-sizing: border-box;
color: #000000;
padding-left: 10px;
border: 1px solid #A3A3A3;
}
.searchInFous{
border: 1px solid #57C4F6;
box-shadow: 0px 0px 5px #57C4F6;
}
.f-searchIn > span{
display: block;
width: 28px;
height: 28px;
background-image: url(../../../static/upDown.png);
background-size: 100% 100%;
background-repeat: no-repeat;
background-position: 0px -13px;
position: absolute;
top: 10px;
right: 5px;
}
.f-searchIn .searchActive{
background-position: 0px 12px;
top: -2px;
}
.f-search .f-searchXl{
position: absolute;
width: 100%;
height: auto;
max-height: 220px;
top: 41px;
left: -1px;
border-radius: 5px;
/*border: 1px solid #D9D9D9;*/
background-color: white;
overflow-x: hidden;
overflow-y: scroll;
}
.f-search .f-searchXl > div{
height: 35px;
line-height: 38px;
color: #000000;
padding-left: 25px;
font-size: 0.92rem;
}
.f-search .f-searchXl > div:hover{
background-color: #D5F1FD;
}
</style>
四、代碼詳解
1. 先說一下 props
我們?cè)诟附M件中需要將子組件需要的數(shù)據(jù)導(dǎo)入,用法如下:
<search @selectFunc="selectFunc" :searchList="searchList" :selectValue="selectValue"></search>
:searchList ="searchList" 就是我們的數(shù)據(jù),這個(gè)可以寫多個(gè)。這里我傳輸了2個(gè)參數(shù)過去,主要是做數(shù)據(jù)修改的說明。大家可以先忽略。
在子組件中,我們的接收和使用方法如下:
props: {
searchList: Array,
selectValue: Object
},
mounted() {
this.data = this.searchList
},
我們?cè)?props 中接收數(shù)據(jù),注意props對(duì)象里面 鍵值 是對(duì)改數(shù)據(jù)的 數(shù)據(jù)類型 的規(guī)定。做了規(guī)范,使用者就只能傳輸指定類型的數(shù)據(jù),否則報(bào)警告
二props對(duì)象中的數(shù)據(jù),我們可以直接在當(dāng)前組件中使用 this.searchList,可以直接使用。至于原理嘛,不懂的可以取腦補(bǔ)一下 js的原型 。os:這些基礎(chǔ),在這就不做詳述了
以上就是props傳遞過來的數(shù)據(jù)的使用了。
2. emit的使用(如何暴露組件方法)
我們已經(jīng)會(huì)使用 父組件向子組件傳數(shù)據(jù)了,那如子組件如何來修改父組件的數(shù)據(jù)呢?
這里提供 2 種實(shí)現(xiàn)方法,但是 第一種不推薦,強(qiáng)烈不推薦
方式一:
selectValue: {
data: '1'
},
。。。。。。。。。。。。。。。
this.selectValue.data = '我被修改了'
即,父組件將 對(duì)象 數(shù)據(jù)傳遞給子組件,子組件直接修改props過來的對(duì)象的值
可以實(shí)現(xiàn),感覺是一個(gè)比較快捷的方式。但是不推薦,這種方式寫多了,容易出錯(cuò),特別是多層組件嵌套的時(shí)候。這種修改對(duì)代碼的迭代和錯(cuò)誤的捕捉都不友好,所以建議大家別這樣寫。
他的實(shí)現(xiàn)原理簡(jiǎn)單提一下: 這個(gè)對(duì)象、數(shù)組啦,是引用數(shù)據(jù)類型,說白了,就是存儲(chǔ)單元的信息是指針,真正數(shù)據(jù)在別的地方,通過指針查詢的數(shù)據(jù),所以這樣寫,對(duì)瀏覽器來說僅僅是傳遞了一個(gè)指針,數(shù)據(jù)還是同一份數(shù)據(jù)。所以你能修改。
方式二:
正兒八經(jīng)的通過 $emit 方法去掉父組件的方法,在父組件中修改data的數(shù)據(jù)。(根正苗紅的方法,規(guī)范寫法)
// 子組件
this.$emit('selectFunc', value)
// 父組件
<search @selectFunc="selectFunc" :searchList="searchList" :selectValue="selectValue"></search>
selectFunc(value) {
this.selectValue2 = value
console.log(this.selectValue)
console.log(this.selectValue2)
}
將父組件的方法注入子組件 @selectFunc ="selectFunc" ,然后在子組件中通過 $emit 調(diào)用他,并傳遞參數(shù)。達(dá)到修改的目的。
五、 總結(jié)
這里主要是總結(jié)一下vue組件封裝的思路,幫大家梳理一下。很簡(jiǎn)單,和jQuery插件、react組件一樣,所有組件都是一個(gè)套路,就是 函數(shù)思想。
組件就是做烤腸臺(tái)機(jī)器,我放進(jìn)去豬肉,再按一下各種開關(guān),然后你給我烤腸。
1. 定義好 你需要使用者傳入的數(shù)據(jù)
2. 定義好 你提供給使用者的方法
3. 寫好組件的內(nèi)部邏輯
這就OK了,一個(gè)完美的,可復(fù)用的組件就完成了。 os: 在此吐槽一下,那些自認(rèn)為是優(yōu)秀的組件,其實(shí),別人拿著沒法用的代碼。
以上所述是小編給大家介紹的vue2.0 自定義組件的方法(vue組件的封裝),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue中的百度地圖定位BMap.GeolocationControl的用法
BMap.GeolocationControl?是百度地圖API中的一個(gè)類,用于添加地理定位控件到地圖上,以便用戶可以通過該控件獲取自己的當(dāng)前位置,本文給大家介紹Vue中的百度地圖定位BMap.GeolocationControl的用法,感興趣的朋友跟隨小編一起看看吧2023-10-10
vue項(xiàng)目實(shí)例中用query傳參如何實(shí)現(xiàn)跳轉(zhuǎn)效果
這篇文章主要介紹了vue項(xiàng)目實(shí)例中用query傳參如何實(shí)現(xiàn)跳轉(zhuǎn)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue v-for直接循環(huán)數(shù)字實(shí)例
今天小編就為大家分享一篇vue v-for直接循環(huán)數(shù)字實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue.js過濾器+ajax實(shí)現(xiàn)事件監(jiān)聽及后臺(tái)php數(shù)據(jù)交互實(shí)例
這篇文章主要介紹了vue.js過濾器+ajax實(shí)現(xiàn)事件監(jiān)聽及后臺(tái)php數(shù)據(jù)交互,結(jié)合實(shí)例形式分析了vue.js前臺(tái)過濾器與ajax后臺(tái)數(shù)據(jù)交互相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
vue清除瀏覽器全部cookie的問題及解決方法(絕對(duì)有效!)
最近項(xiàng)目要實(shí)現(xiàn)關(guān)閉瀏覽器清除用戶緩存的功能,下面這篇文章主要給大家介紹了關(guān)于vue清除瀏覽器全部cookie的問題及解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
快速解決 keep-alive 緩存組件中定時(shí)器干擾問題
文章介紹了在使用keep-alive緩存組件時(shí),如何在組件被緩存后清理定時(shí)器以避免干擾其他組件的邏輯,通過在deactivated鉤子中清理定時(shí)器,可以確保組件被緩存時(shí)不會(huì)繼續(xù)運(yùn)行定時(shí)器,感興趣的朋友一起看看吧2025-02-02
Vue實(shí)現(xiàn)開始時(shí)間和結(jié)束時(shí)間范圍查詢
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)開始時(shí)間和結(jié)束時(shí)間的范圍查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
快速搭建vue2.0+boostrap項(xiàng)目的方法
這篇文章主要介紹了快速搭建vue2.0+boostrap項(xiàng)目的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

