欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue2.0 自定義組件的方法(vue組件的封裝)

 更新時間:2018年06月05日 08:26:44   作者:Mr.聶  
這篇文章主要介紹了vue2.0 自定義組件的方法(vue組件的封裝),本文通過實例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下

一、前言

之前的博客聊過 vue2.0和react的技術(shù)選型;聊過vue的axios封裝和vuex使用。今天簡單聊聊 vue 組件的封裝。

vue 的ui框架現(xiàn)在是很多的,但是鑒于移動設(shè)備的復(fù)雜性,兼容性問題突出。像 Mint-UI 等說實話很不錯了,但是坑也是不少,而且很多功能也是僅憑這些實現(xiàn)不了,這需要我們?nèi)シ庋b自己的可復(fù)用組件

二、封裝組件的步驟

1. 建立組件的模板,先把架子搭起來,寫寫樣式,考慮你的組件的基本邏輯。

os:思考1小時,碼碼10分鐘,程序猿的準(zhǔn)則。

2. 準(zhǔn)備組件的好數(shù)據(jù)輸入。即分析好邏輯,定好 props 里面的數(shù)據(jù)、類型。(后面詳解)

3.準(zhǔn)備組件的好數(shù)據(jù)輸出。即根據(jù)組件邏輯,做好要暴露出來的方法。(后面詳解)

4.封裝完畢了,直接調(diào)用即可。

os: 代碼可以不看,結(jié)論在文章最后

接下來以一個很簡單的例子具體說明一下

現(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: ['草船借箭', '大富翁', '測試數(shù)據(jù)'],
 // 直接通過props傳遞對象 修改,挺便捷的,但是不規(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

我們在父組件中需要將子組件需要的數(shù)據(jù)導(dǎo)入,用法如下:

<search @selectFunc="selectFunc" :searchList="searchList" :selectValue="selectValue"></search>

:searchList ="searchList"  就是我們的數(shù)據(jù),這個可以寫多個。這里我傳輸了2個參數(shù)過去,主要是做數(shù)據(jù)修改的說明。大家可以先忽略。

在子組件中,我們的接收和使用方法如下:

props: {
 searchList: Array,
 selectValue: Object
 },
mounted() {
 this.data = this.searchList
 },

我們在 props 中接收數(shù)據(jù),注意props對象里面 鍵值 是對改數(shù)據(jù)的 數(shù)據(jù)類型 的規(guī)定。做了規(guī)范,使用者就只能傳輸指定類型的數(shù)據(jù),否則報警告

二props對象中的數(shù)據(jù),我們可以直接在當(dāng)前組件中使用   this.searchList,可以直接使用。至于原理嘛,不懂的可以取腦補一下 js的原型 。os:這些基礎(chǔ),在這就不做詳述了

以上就是props傳遞過來的數(shù)據(jù)的使用了。

2. emit的使用(如何暴露組件方法)

我們已經(jīng)會使用 父組件向子組件傳數(shù)據(jù)了,那如子組件如何來修改父組件的數(shù)據(jù)呢?

這里提供 2 種實現(xiàn)方法,但是 第一種不推薦,強烈不推薦

方式一:

   selectValue: {
  data: '1'
 },
    。。。。。。。。。。。。。。。
 this.selectValue.data = '我被修改了'

即,父組件將 對象 數(shù)據(jù)傳遞給子組件,子組件直接修改props過來的對象的值

可以實現(xiàn),感覺是一個比較快捷的方式。但是不推薦,這種方式寫多了,容易出錯,特別是多層組件嵌套的時候。這種修改對代碼的迭代和錯誤的捕捉都不友好,所以建議大家別這樣寫。

他的實現(xiàn)原理簡單提一下: 這個對象、數(shù)組啦,是引用數(shù)據(jù)類型,說白了,就是存儲單元的信息是指針,真正數(shù)據(jù)在別的地方,通過指針查詢的數(shù)據(jù),所以這樣寫,對瀏覽器來說僅僅是傳遞了一個指針,數(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組件封裝的思路,幫大家梳理一下。很簡單,和jQuery插件、react組件一樣,所有組件都是一個套路,就是 函數(shù)思想。

組件就是做烤腸臺機器,我放進(jìn)去豬肉,再按一下各種開關(guān),然后你給我烤腸。

1. 定義好 你需要使用者傳入的數(shù)據(jù)

2. 定義好 你提供給使用者的方法

3. 寫好組件的內(nèi)部邏輯

這就OK了,一個完美的,可復(fù)用的組件就完成了。    os: 在此吐槽一下,那些自認(rèn)為是優(yōu)秀的組件,其實,別人拿著沒法用的代碼。

以上所述是小編給大家介紹的vue2.0 自定義組件的方法(vue組件的封裝),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 關(guān)于vue二進(jìn)制轉(zhuǎn)圖片顯示問題 后端返回的是byte[]數(shù)組

    關(guān)于vue二進(jìn)制轉(zhuǎn)圖片顯示問題 后端返回的是byte[]數(shù)組

    這篇文章主要介紹了關(guān)于vue二進(jìn)制轉(zhuǎn)圖片顯示問題 后端返回的是byte[]數(shù)組,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 在vue中高德地圖引入和軌跡的繪制的實現(xiàn)

    在vue中高德地圖引入和軌跡的繪制的實現(xiàn)

    這篇文章主要介紹了在vue中高德地圖引入和軌跡的繪制的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 解決vue watch數(shù)據(jù)的方法被調(diào)用了兩次的問題

    解決vue watch數(shù)據(jù)的方法被調(diào)用了兩次的問題

    這篇文章主要介紹了解決vue watch數(shù)據(jù)的方法被調(diào)用了兩次的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • VeeValidate 的使用場景以及配置詳解

    VeeValidate 的使用場景以及配置詳解

    這篇文章主要介紹了VeeValidate 的使用場景以及配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • vue組件component的注冊與使用詳解

    vue組件component的注冊與使用詳解

    組件是Vue是一個可以重復(fù)使用的Vue實例, 它擁有獨一無二的組件名稱,它可以擴展HTML元素,以組件名稱的方式作為自定義的HTML標(biāo)簽,這篇文章主要介紹了vue組件component的注冊與使用,需要的朋友可以參考下
    2022-08-08
  • 如何使用fetchEventSource實現(xiàn)sse流式請求

    如何使用fetchEventSource實現(xiàn)sse流式請求

    這篇文章主要介紹了如何使用fetchEventSource實現(xiàn)sse流式請求問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • vue實現(xiàn)后臺管理權(quán)限系統(tǒng)及頂欄三級菜單顯示功能

    vue實現(xiàn)后臺管理權(quán)限系統(tǒng)及頂欄三級菜單顯示功能

    這篇文章主要介紹了vue實現(xiàn)后臺管理權(quán)限系統(tǒng)及頂欄三級菜單顯示功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • vite創(chuàng)建一個標(biāo)準(zhǔn)vue3+ts+pinia項目

    vite創(chuàng)建一個標(biāo)準(zhǔn)vue3+ts+pinia項目

    本文主要介紹了vite創(chuàng)建一個標(biāo)準(zhǔn)vue3+ts+pinia項目,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • vue/cli?配置動態(tài)代理無需重啟服務(wù)的操作方法

    vue/cli?配置動態(tài)代理無需重啟服務(wù)的操作方法

    vue-cli是vue.js的腳手架,用于自動生成vue.js+webpack的項目模板,分為vue?init?webpack-simple?項目名和vue?init?webpack?項目名兩種,這篇文章主要介紹了vue/cli?配置動態(tài)代理,無需重啟服務(wù),需要的朋友可以參考下
    2022-05-05
  • Vue+Canvas繪圖使用的講解

    Vue+Canvas繪圖使用的講解

    這篇文章主要介紹了Vue+Canvas繪圖的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評論