vue 基于組件的二次封裝

組件不滿足需求词裤,要對當前組件再次封裝黍判,會遇到以下一些問題:

1豫尽、原組件的Attributes怎么處理原組件屬性可以通過透傳

$attrs,獲取父組件的屬性

<el-input ref="childInputRef" v-bind="$attrs"></el-input>
2顷帖、原組件的插槽怎么設(shè)置

通過$slots獲取當前組件所有的插槽對象美旧,通過遍歷添加到原組件插槽
子組件:searchInput.vue

<el-input ref="childInputRef" v-bind="$attrs">
  <template v-for="(value, name) in $slots" #[name]>
    <slot :name="name"></slot>
  </template>
</el-input>

父組件

<SearchInput >
      <template #append">
        <el-button>Search</el-button>
      </template>
    </SearchInput>

效果:


1687939543818.png

具名作用域插槽
子組件:searchInput.vue

<el-input ref="childInputRef" v-bind="$attrs">
  <template v-for="(value, name) in $slots" #[name]>
    <slot :name="name" :slotData="slotData">></slot>
  </template>
</el-input>
...
....
const slotData = reactive({
  msg: 'aaaa'
})

父組件

<SearchInput>
  <template #append="{slotData}">
    {{ slotData.msg }}
   <el-button>Search</el-button>
  </template>
</SearchInput>

效果:


1687939766543.png

3渤滞、原組件的ref方法調(diào)用
將原組件的提供的方法加入到當前組件中
子組件:searchInput.vue

<el-input ref="childInputRef" v-bind="$attrs"></el-input>
<script setup lang="ts">
import { useAttrs, ref, onMounted, reactive, useSlots } from 'vue'

const childInputRef = ref()
const options = reactive<{[key: string]: any}>({})

const slotData = reactive({
  msg: 'aaaa'
})

onMounted(() => {
  const entries = Object.entries(childInputRef?.value)
  console.log('entries:', entries)
  for (const [key, value] of entries) {
    if (!value || typeof value !== 'function') {
      continue
    }
    options[key] = value
  }
})

父組件:

<template>
  <div>
    <SearchInput
      ref="searchInput" 
  >
    </SearchInput>
    <el-button @click="changeFocus">Focus</el-button>
  </div>
</template>

<script setup lang="ts"> 
const searchInput = ref()
function changeFocus() {
  searchInput.value.focus()
}
</script>
完整代碼:

子組件:searchInput.vue

<template>
  <div>
    <el-input ref="childInputRef" v-bind="$attrs">
      <template v-for="(value, name) in slots" #[name]>
        <slot :name="name" :slotData="slotData"></slot>
      </template>
    </el-input>
  </div>
</template>

<script setup lang="ts">
import { useAttrs, ref, onMounted, reactive, useSlots } from 'vue'

// const attrs = useAttrs()
const slots = useSlots()
const childInputRef = ref()
const options = reactive<{[key: string]: any}>({})

const slotData = reactive({
  msg: 'aaaa'
})

onMounted(() => {
  const entries = Object.entries(childInputRef?.value)
  console.log('entries:', entries)
  for (const [key, value] of entries) {
    if (!value || typeof value !== 'function') {
      continue
    }
    options[key] = value
  }
})

defineExpose(options)
</script>

父組件:index.vue

<template>
  <div>
    <SearchInput
      ref="searchInput" 
      v-model="searchForm.keyword"  
      placeholder="請輸入"
      active-value
      show-password
      clearable
      @change="handleChange">
      <template #append="{slotData}">
        {{ slotData.msg }}
        <el-button>Search</el-button>
      </template>
    </SearchInput>
    <el-button @click="changeFocus">Focus</el-button>
  </div>
</template>

<script setup lang="ts"> 
import SearchInput from './views/layout/components/searchInput.vue'
import { onMounted, reactive, ref, toRef, toRefs, getCurrentInstance } from 'vue';

const searchInput = ref()
const searchForm = reactive({
  keyword: ''
})

function changeFocus() {
  console.log('keyword:', searchForm.keyword)
  searchInput.value.focus()
}
function handleChange(v: string) {
  console.log('input:', v)
}

</script>

<style lang="scss" scoped>
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市榴嗅,隨后出現(xiàn)的幾起案子妄呕,更是在濱河造成了極大的恐慌,老刑警劉巖嗽测,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件趴腋,死亡現(xiàn)場離奇詭異,居然都是意外死亡论咏,警方通過查閱死者的電腦和手機优炬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來厅贪,“玉大人蠢护,你說我怎么就攤上這事⊙蹋” “怎么了葵硕?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長贯吓。 經(jīng)常有香客問我懈凹,道長,這世上最難降的妖魔是什么悄谐? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任介评,我火速辦了婚禮,結(jié)果婚禮上爬舰,老公的妹妹穿的比我還像新娘们陆。我一直安慰自己,他們只是感情好情屹,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布坪仇。 她就那樣靜靜地躺著,像睡著了一般垃你。 火紅的嫁衣襯著肌膚如雪椅文。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天惜颇,我揣著相機與錄音皆刺,去河邊找鬼。 笑死官还,一個胖子當著我的面吹牛芹橡,可吹牛的內(nèi)容都是我干的毒坛。 我是一名探鬼主播望伦,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼林说,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了屯伞?” 一聲冷哼從身側(cè)響起腿箩,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎劣摇,沒想到半個月后珠移,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡末融,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年钧惧,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勾习。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡浓瞪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出巧婶,到底是詐尸還是另有隱情乾颁,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布艺栈,位于F島的核電站英岭,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏湿右。R本人自食惡果不足惜诅妹,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望毅人。 院中可真熱鬧漾唉,春花似錦、人聲如沸堰塌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽场刑。三九已至般此,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間牵现,已是汗流浹背铐懊。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瞎疼,地道東北人科乎。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像贼急,于是被迫代替她去往敵國和親茅茂。 傳聞我的和親對象是個殘疾皇子捏萍,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容