国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Vue-組件二次封裝

這篇具有很好參考價值的文章主要介紹了Vue-組件二次封裝。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

本次對el-input進(jìn)行簡單封裝進(jìn)行演示

  • 封裝很簡單,就給激活樣式的邊框(主要是功能)

本次封裝主要使用到vue自帶的幾個對象

  • $attrs:獲取綁定在組件上的所有屬性
  • $listeners: 獲取綁定在組件上的所有函數(shù)方法
  • $slots: 獲取應(yīng)用在組件內(nèi)的所有插槽

1、屬性傳遞

element 的input組件有很多屬性,

  • 想要實現(xiàn)在封裝好后的組件上使用el-input組件的屬性,會直接傳遞到el-input組件上,包括v-model。
  • 在組件中,可以使用this.$attrs獲取所有綁定在組件上的屬性(不包括方法)
  • 這樣,我們就可以在封裝的組件內(nèi),使用v-bind="$attrs",直接把屬性傳遞到內(nèi)部組件上。
  • 在下列案例中,由于v-model:value 和 @input兩個組合的語法糖,$attrs只能獲取屬性,所以只能傳遞:value屬性

1.1、父組件

<template>
  <div class="wrapper"> 
    <my-input v-model="val">
    </my-input>
  </div>
</template>

<script>
  import MyInput from '@/components/MyInput'
  export default {
    components: {
      MyInput,
    },
    data() {
      return {
        val: '111',
      }
    },
    methods: {
      inputChange(val){
        console.log(val);
      }
    }
  }
</script>

<style lang="scss" scoped>
  .wrapper {
    padding: 10vh;
  }
</style>

1.2、子組件

<template>
  <el-input v-bind="$attrs"></el-input>
</template>
<script>
  export default {
    created() {
      console.log("attrs:",this.$attrs);
    }
  }
</script>
<style lang="scss" scoped>
::v-deep {
  .el-input__inner:focus {
    border-color: red;
}
}

</style>

1.3、效果

  • 這時候給輸入框輸入值是無效的,因為目前只能把value屬性綁定到el-input上,并沒有把input函數(shù)綁定上去,所以不能修改父組件傳過來的value的值。

Vue-組件二次封裝,vue.js,javascript,前端

2、方法傳遞

element的組件,也有很多方法,比如:change等函數(shù)

  • 想要實現(xiàn)在封裝好后的組件上使用el-input組件的方法,會直接傳遞到el-input組件上。
  • 在組件中,可以使用this.$listeners獲取所有綁定在組件上的屬性(不包括屬性)
  • 這樣,我們就可以在封裝的組件內(nèi),使用v-on="$listeners",直接把方法傳遞到內(nèi)部組件上。
  • 在下列案例中,由于v-model:value 和 @input兩個組合的語法糖,$listeners只能獲取屬性,所以結(jié)合上面$attrsjiu可以完整的實現(xiàn)v-model的效果了

2.1、父組件

<template>
  <div class="wrapper"> 
    <my-input v-model="val" @change="inputChange">
    </my-input>
  </div>
</template>

<script>
  import MyInput from '@/components/MyInput'
  export default {
    components: {
      MyInput,
    },
    data() {
      return {
        val: '111',
      }
    },
    methods: {
      inputChange(val){
        console.log("inputChange:", val);
      }
    }
  }
</script>

2.2、子組件

<template>
  <el-input v-bind="$attrs" v-on="$listeners"></el-input>
</template>
<script>
  export default {
    created() {
      console.log("attrs:",this.$attrs);
      console.log("listeners:",this.$listeners);
    }
  }
</script>
<style lang="scss" scoped>
::v-deep {
  .el-input__inner:focus {
    border-color: red;
  }
}

</style>

2.3、效果

這時候搭配$attrs就可以實現(xiàn)v-model的完整效果了,以及@change函數(shù)也會傳遞過去

Vue-組件二次封裝,vue.js,javascript,前端

3、插槽傳遞

element的組件,也包括了很多的插槽

  • 想要給封裝好后的組件,使用的插槽,傳遞到el-input
  • 在組件中,可以使用this.$slots獲取所有綁定在組件上的插槽
  • 這樣,我們就可以在封裝的組件內(nèi),使用v-for="(val, key) in $slots",所有插槽,遍歷放到組件中,當(dāng)作組件的插槽
  • 注意插槽傳參也要處理(我這里沒處理)

3.1、父組件

<template>
  <div class="wrapper"> 
    <my-input v-model="val" @change="inputChange">
      <template slot="prepend">Http://</template>
      <el-button slot="append" icon="el-icon-search"></el-button>
    </my-input>
  </div>
</template>

<script>
  import MyInput from '@/components/MyInput'
  export default {
    components: {
      MyInput,
    },
    data() {
      return {
        val: '111',
      }
    },
    methods: {
      inputChange(val){
        console.log("inputChange:", val);
      }
    }
  }
</script>

<style lang="scss" scoped>
  .wrapper {
    padding: 10vh;
  }
</style>

3.2、子組件

<template>
  <el-input v-bind="$attrs" v-on="$listeners">
    <template v-for="(val, key) in $slots">
      <slot :name="key"></slot>
    </template>
  </el-input>
</template>
<script>
  export default {
    created() {
      console.log("attrs:",this.$attrs);
      console.log("listeners:",this.$listeners);
      console.log("slots",this.$slots);
    }
  }
</script>
<style lang="scss" scoped>
::v-deep {
  .el-input__inner:focus {
    border-color: red;
  }
}

</style>

3.3、效果

Vue-組件二次封裝,vue.js,javascript,前端文章來源地址http://www.zghlxwxcb.cn/news/detail-629824.html

4、ref偽傳遞(適用于vue3)

  • 為什么說偽傳遞呢,因為在vue中,根本就拿不到外層組件的ref屬性,所以只能另換思路
  • 你要用ref,無非就是想調(diào)用組件里面的函數(shù)。那我封裝的組件里面,可以把被封裝的組件的函數(shù),直接提取出來,當(dāng)作我封裝組件的函數(shù)即可實現(xiàn)
  • 適用于Vue3,vue2會卡死

4.1、父組件

<template>
  <div class="wrapper"> 
    <my-input ref="muInput" v-model="val" @change="inputChange">
      <template slot="prepend">Http://</template>
      <el-button slot="append" icon="el-icon-search"></el-button>
    </my-input>
  </div>
</template>

<script>
  import MyInput from '@/components/MyInput'
  export default {
    components: {
      MyInput,
    },
    data() {
      return {
        val: '111',
      }
    },
    mounted() {
      this.$refs.muInput.focus()
    },
    methods: {
      inputChange(val){
        console.log("inputChange:", val);
      }
    }
  }
</script>

<style lang="scss" scoped>
  .wrapper {
    padding: 10vh;
  }
</style>

4.2、子組件

<template>
  <el-input ref="input" v-bind="$attrs" v-on="$listeners">
    <template v-for="(val, key) in $slots" #[key]>
      <slot :name="key"></slot>
    </template>
  </el-input>
</template>
<script>
  export default {
    mounted() {
      console.log("attrs:",this.$attrs);
      console.log("listeners:",this.$listeners);
      console.log("slots",this.$slots);

      for (const [key, value] of Object.entries(this.$refs.input)) {
        this[key] = value
      }
    }
  }
</script>
<style lang="scss" scoped>
::v-deep {
  .el-input__inner:focus {
    border-color: red;
  }
}

</style>

5、插槽傳遞補(bǔ)充22

  • $slots可以獲取所有應(yīng)用在子組件上的插槽。但是僅限于子組件不使用插槽進(jìn)行傳參(作用域插槽)只能捕獲到具名插槽。
  • 當(dāng)需要使用作用域插槽時,就會發(fā)現(xiàn),$solts捕獲不到這個插槽了。
  • 這時候,就需要使用$scopedSlots,它就等于具名插槽 + 作用域插槽,捕獲的更加全面。
  • 上面我們只是使用$slots傳遞具名插槽,如果插槽需要傳參,則無法使用,我們要換成更加全面的$scopedSlots

5.1、子組件

<template>
  <el-input v-bind="$attrs" v-on="$listeners">
    <template v-for="(val, key) in $scopedSlots" :scope="scope">
      <slot :name="key" :data="scope"></slot>
    </template>
  </el-input>
</template>
<script>
  export default {
    created() {
      console.log("attrs:",this.$attrs);
      console.log("listeners:",this.$listeners);
      console.log("slots",this.$slots);
      console.log("scopedSlots",this.$scopedSlots);
    }
  }
</script>
<style lang="scss" scoped>
::v-deep {
  .el-input__inner:focus {
    border-color: red;
  }
}

</style>

到了這里,關(guān)于Vue-組件二次封裝的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • element ui 表格組件與分頁組件的二次封裝
【擴(kuò)展】vue中的render函數(shù)

    element ui 表格組件與分頁組件的二次封裝 【擴(kuò)展】vue中的render函數(shù)

    目錄 效果圖? 組件封裝 ?parseTime函數(shù) debounce?函數(shù) render通用渲染模版 頁面使用 【擴(kuò)展】vue 函數(shù)式組件 函數(shù)式組件特點: 函數(shù)式組件的優(yōu)點: 【擴(kuò)展】vue中的render函數(shù) 一、初步認(rèn)識render函數(shù) 二、為什么使用render函數(shù) 三、render函數(shù)的解析 【擴(kuò)展】添加操作欄顯示權(quán)限 結(jié)構(gòu)

    2024年02月09日
    瀏覽(41)
  • 記錄--封裝一個通過js調(diào)用的全局vue組件

    記錄--封裝一個通過js調(diào)用的全局vue組件

    在使用vue項目編寫的時候,不可避免的會碰到需要時js api來調(diào)用組件進(jìn)行顯示的情況 例如餓了么element ui 的 Notification 通知、Message 消息提示等組件 雖然已經(jīng)提供了,但是由于api的限制,我們只能通過特定的參數(shù)來有限的改變組件的樣式 之前的文章說過可以使用 new Vue() 、

    2024年02月09日
    瀏覽(33)
  • Vue + Element UI 前端篇(七):功能組件封裝

    Vue + Element UI 前端篇(七):功能組件封裝

    為了避免組件代碼的臃腫,這里對主要的功能部件進(jìn)行封裝,保證代碼的模塊化和簡潔度。 組件結(jié)構(gòu) 組件封裝重構(gòu)后,試圖組件結(jié)構(gòu)如下圖所示 代碼一覽 Home組件被簡化,包含導(dǎo)航、頭部和主內(nèi)容三個組件。 Home.vue HeadBar.vue MenuBar.vue Main.vue 國際化語言切換也被封裝成為了組

    2024年02月09日
    瀏覽(27)
  • vue2+ant-design-vue a-select組件二次封裝(支持單選/多選添加全選/分頁(多選跨頁選中)/自定義label)

    vue2+ant-design-vue a-select組件二次封裝(支持單選/多選添加全選/分頁(多選跨頁選中)/自定義label)

    參數(shù) 說明 類型 默認(rèn)值 v-model 綁定值 boolean / string / number/Array - mode 設(shè)置’multiple’\\\'tags’多選 (顯示全選) String - optionSource 下拉數(shù)據(jù)源 Array - width select寬度(可以設(shè)置百分比或px) String 100% customLabel 是否自定義設(shè)置下拉label String - valueKey 傳入的 option 數(shù)組中,要作為最終選擇

    2024年02月08日
    瀏覽(31)
  • 前端Vue自定義簡單實用輪播圖封裝組件 快速實現(xiàn)輪播圖

    前端Vue自定義簡單實用輪播圖封裝組件 快速實現(xiàn)輪播圖

    前端Vue自定義簡單實用輪播圖封裝組件 快速實現(xiàn)輪播圖, 閱讀全文下載完整代碼請關(guān)注微信公眾號: 前端組件開發(fā) 效果圖如下: 使用方法 HTML代碼實現(xiàn)部分

    2024年02月10日
    瀏覽(37)
  • vue+element-ui el-table組件二次封裝實現(xiàn)虛擬滾動,解決數(shù)據(jù)量大渲染DOM過多而卡頓問題

    vue+element-ui el-table組件二次封裝實現(xiàn)虛擬滾動,解決數(shù)據(jù)量大渲染DOM過多而卡頓問題

    某些頁面不做分頁時,當(dāng)數(shù)據(jù)過多,會導(dǎo)致頁面卡頓,甚至卡死 一、固定一個 可視區(qū)域 的大小并且其大小是不變的,那么要做到性能最大化就需要盡量少地渲染 DOM 元素,而這個最小值也就是可視范圍內(nèi)需要展示的內(nèi)容,而可視區(qū)域之外的元素均可以不做渲染。 二、如何計

    2024年02月10日
    瀏覽(18)
  • 基于VUE3+Layui從頭搭建通用后臺管理系統(tǒng)(前端篇)十一:通用表單組件封裝實現(xiàn)

    基于VUE3+Layui從頭搭建通用后臺管理系統(tǒng)(前端篇)十一:通用表單組件封裝實現(xiàn)

    ??本章實現(xiàn)通用表單組件,根據(jù)實體配置識別實體屬性,并自動生成編輯組件,實現(xiàn)對應(yīng)數(shù)據(jù)填充、校驗及保存等邏輯。 1. 詳細(xì)課程地址: https://edu.csdn.net/course/detail/38183 2. 源碼下載地址: 點擊下載

    2024年02月10日
    瀏覽(29)
  • 基于VUE3+Layui從頭搭建通用后臺管理系統(tǒng)(前端篇)八:自定義組件封裝上

    基于VUE3+Layui從頭搭建通用后臺管理系統(tǒng)(前端篇)八:自定義組件封裝上

    ??本章實現(xiàn)一些自定義組件的封裝,包括數(shù)據(jù)字典組件的封裝、下拉列表組件封裝、復(fù)選框單選框組件封裝、單選框組件封裝、文件上傳組件封裝、級聯(lián)選擇組件封裝、富文本組件封裝等。 1. 詳細(xì)課程地址: https://edu.csdn.net/course/detail/38183 2. 源碼下載地址: 點擊下載

    2024年02月12日
    瀏覽(26)
  • vue中axios的二次封裝——vue 封裝axios詳細(xì)步驟

    vue中axios的二次封裝——vue 封裝axios詳細(xì)步驟

    ? ? api統(tǒng)一管理,不管接口有多少,所有的接口都可以非常清晰,容易維護(hù)。 ? ? 通常我們的項目會越做越大,頁面也會越來越多,如果頁面非常的少,直接用axios也沒有什么大的影響,那頁面組件多了起來,上百個接口呢,這個時候后端改了接口,多加了一個參數(shù)什么的呢

    2024年02月02日
    瀏覽(121)
  • 【二開】JeecgBoot-vue3二次開發(fā) 前端 擴(kuò)展online表單js增強(qiáng)等-在表單里拿到列表上下文onlineTableContext

    【二開】JeecgBoot-vue3二次開發(fā) 前端 擴(kuò)展online表單js增強(qiáng)等-在表單里拿到列表上下文onlineTableContext

    【二開】JeecgBoot-vue3二次開發(fā) 前端 擴(kuò)展online表單js增強(qiáng)等-在表單里拿到列表上下文 對應(yīng)的屬性方法 acceptHrefParams \\\"p 跳轉(zhuǎn)時獲取的參數(shù)信息\\\" currentPage \\\"p 當(dāng)前頁數(shù)\\\" currentTableName \\\"p 當(dāng)前表名\\\" description \\\"p 當(dāng)前表描述\\\" hasChildrenField \\\"p 是否有子節(jié)點的字段名,僅樹形表單下有效\\\" is

    2024年02月15日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包