日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

element vue 获取select 的label_Vue动态组件component的深度使用

發(fā)布時間:2024/9/19 vue 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 element vue 获取select 的label_Vue动态组件component的深度使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

背景介紹

最近在封裝一些基于Vue+ElementUI的組件,將一些實際項目中常用的,有一定規(guī)律的業(yè)務進行抽象總結,開發(fā)出相應的Vue組件。

組件封裝

首先想到的就是Form組件,在Element UI提供的Form中,我們需要一個一個的去添加對用的FormItem

<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="活動名稱">
<el-input v-model="form.name">el-input>
el-form-item>
<el-form-item label="活動區(qū)域">
<el-select v-model="form.region" placeholder="請選擇活動區(qū)域">
<el-option label="區(qū)域一" value="shanghai">el-option>
<el-option label="區(qū)域二" value="beijing">el-option>
el-select>
el-form-item>
el-form>

可以發(fā)現(xiàn),每個FormItem的結構都是一樣的,只是其中包含的組件不一樣。這樣,我們就可以通過封裝組件的方式,將代碼簡化,

<el-form ref="form" :model="form" label-width="80px">
<my-form-itemlabel="活動名稱"v-model="form.name"widget="input">my-form-item>
<my-form-item label="活動區(qū)域" v-model="form.region" placeholder="請選擇活動區(qū)域"widget="select":options="options">my-form-item>
el-form>


**my-form-item.vue **0.1版如下

<el-form-item :label="title">
<el-select v-if="widget === 'select'" v-model="form[path]" :clearable="true">
<el-option v-for="option in options" :key="option.code" :label="option.name" :value="option.code">el-option>
el-select>
<el-input v-else-if="widget === 'input'" v-model="form[path]" :clearable="true">el-input>
el-form-item>

0.1版,直接使用了v-if來處理widget類型,此時,input和select的邏輯耦合在了一起,再需要其他的組件類型時,這個文件結構會很復雜。

Vue動態(tài)組件

在Vue中,提供了動態(tài)組件component,它可以在動態(tài)的選擇加載那個組件。
例子:

<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png" />


<a href="#" @click="current = 'hello-world'">Hello Worlda>
<a href="#" @click="current = 'hello-world2'">Hello 2a>
<component :is="current" :msg="current" @clicked="handleClicked">component>
div>
template>

<script>// @ is an alias to /srcimport HelloWorld from '../components/HelloWorld.vue';import HelloWorld2 from '../components/HelloWorld2';export default {name: 'Home',components: {
HelloWorld,
HelloWorld2
},
data() {return {current: 'hello-world'
};
},methods: {
handleClicked(e) {
alert(e);
}
}
};

在component中 :is?屬性是必須的,它的內(nèi)容為在template中使用的組件標簽。通過 :is?內(nèi)容的切換就可以動態(tài)的渲染和組件了。

改造組件

my-form-item.vue

<componentv-if="widgetType":is="currentWidget"v-model="form[path]"v-bind="$props":label="title":property="mProperty"
>component>

my-input.vue

<template>
<el-form-item :label="label">
<el-input :value="value" @input="handleInput">el-input>
el-form-item>
template>

my-select.vue

<template>
<el-form-item :label="label">
<el-select :value="value" :clearable="true" @input="handleInput">
<el-optionv-for="option in widgetOptions":key="option.code":label="option.name":value="option.code"
>el-option>
el-select>
el-form-item>
template>

這樣input和select就分解成了兩個組件,每個組件只需要關注自己的邏輯即可。如果需要擴充新的組件,只要按照規(guī)范創(chuàng)建一個新的組件即可。

總結

動態(tài)組件可以將props完成的傳遞給實際的組件,同樣也可將實際組件的Emit監(jiān)聽到,這樣參數(shù)傳遞的問題就解決了,使用動態(tài)組件就可以像使用實際一樣,開發(fā)體驗上實現(xiàn)零差別,代碼處理邏輯實現(xiàn)解耦

總結

以上是生活随笔為你收集整理的element vue 获取select 的label_Vue动态组件component的深度使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。