vue?elementui?實(shí)現(xiàn)搜索欄子組件封裝的示例代碼
前言
描述: 在基本項(xiàng)目中搜索欄、分頁(yè)組件格式樣式幾乎是固定的,只是對(duì)應(yīng)的數(shù)據(jù)不同,由于模塊會(huì)隨著需求的不斷增加,可能會(huì)導(dǎo)致重復(fù)代碼越來(lái)越多,這時(shí)可以用到子組件,需要將相同的模塊代碼統(tǒng)一封裝,提高開(kāi)發(fā)效率和頁(yè)面統(tǒng)一。(自己的想法,只根據(jù)自己需求來(lái)說(shuō))
需求
實(shí)現(xiàn)
子組件(search.vue)
html
<template> <div class="search-main"> <!-- 下拉框 --> <el-select class="com com-margin" v-model="objSearch.searchType" filterable placeholder="請(qǐng)選擇" > <el-option v-for="item in objSearch.searchKeyOptions" :key="item.value" :label="item.label" :value="item.value" > </el-option> </el-select> <!-- 搜索值 --> <el-input class="com" v-model="objSearch.searchValue" placeholder="請(qǐng)輸入搜索值" ></el-input> <!-- 下拉框 --> <el-cascader class="com" :options="objSearch.options" :props="{ checkStrictly: true }" clearable :placeholder="objSearch.placeholder" v-model="objSearch.status" ></el-cascader> <el-button @click="search" type="primary" icon="el-icon-search" >搜索</el-button > <el-button @click="refresh" type="primary" icon="el-icon-refresh" >刷新</el-button > <el-button type="primary" icon="el-icon-document" @click="exportExcel" >導(dǎo)出EXCEL</el-button > <el-button class="total" type="primary" icon="el-icon-files" >{{ objSearch.name }}總數(shù):{{ objSearch.total }}{{ objSearch.unit }}</el-button > </div> </template>
js
<script> export default { name: 'Search', // 接收父組件對(duì)象 props: { objSearch: { type: Object, default: null } }, methods: { // 搜索 search () { let obj = { searchType: this.objSearch.searchType, searchValue: this.objSearch.searchValue, status: this.objSearch.status } this.$emit('search', obj) }, // 刷新 refresh () { this.$emit('refresh') }, // 導(dǎo)出 exportExcel () { this.$emit('exportExcel') } } } </script>
css
<style scoped> .search-main { width: 100%; line-height: 4rem; } .com { width: 10rem; } .com-margin { margin-left: 1rem; } .total { float: right; margin-top: 1rem; } </style>
父組件部分主要代碼(index.vue)
html(主要代碼)
Search 指components注冊(cè)的標(biāo)簽。
:objSearch 指向子組件傳的參數(shù)
@refresh、@exportExcel、@search 指子組件觸發(fā)的方法
<div class="device-sensor-main"> <!--使用Search子組件 refresh 方法是刷新,exportExcel 方法是導(dǎo)出,search 方法是搜索 --> <div class="search"> <Search :objSearch="objSearch" @refresh="refresh" @exportExcel="exportExcel" @search="search" ></Search> </div> </div>
js(主要代碼)
<script> // 引用搜索模塊子組件 import Search from '@/components/search' export default { name: 'SensorList', // 注冊(cè) components: { Search }, data () { return { // 搜索模塊 objSearch: { searchKeyOptions: [ { label: '傳感器編號(hào)', value: 'sensorImei' }, { label: '設(shè)備IMEI號(hào)', value: 'imei' }, { label: '醫(yī)院名稱', value: 'hospitalName' } ], searchType: 'sensorImei', searchValue: '', name: '設(shè)備', unit: '臺(tái)', total: 0, placeholder: '傳感器狀態(tài)', options: [], status: [] } } }, methods: { // 搜索 obj就是子組件傳過(guò)來(lái)的對(duì)象 search (obj) { console.log('子組件搜索返回?cái)?shù)據(jù):', obj) }, // 刷新 refresh () { console.log('刷新') }, // 導(dǎo)出 exportExcel () { console.log('導(dǎo)出') } } } </script>
css
<style scoped> .search { width: 100%; height: 4rem; } .footer { text-align: center; } .el-table .el-table__cell { padding: 0.3rem 0; } </style>
到此這篇關(guān)于vue elementui 搜索欄子組件封裝的文章就介紹到這了,更多相關(guān)vue elementui 搜索欄組件封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
完美解決element-ui的el-input設(shè)置number類(lèi)型后的相關(guān)問(wèn)題
這篇文章主要介紹了完美解決element-ui的el-input設(shè)置number類(lèi)型后的相關(guān)問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10vue antd的from表單中驗(yàn)證rules中type的坑記錄
這篇文章主要介紹了vue antd的from表單中驗(yàn)證rules中type的坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Vue中判斷語(yǔ)句與循環(huán)語(yǔ)句基礎(chǔ)用法及v-if和v-for的注意事項(xiàng)詳解
在Vue指令中,最經(jīng)常被用于做邏輯操作的指令,下面這篇文章主要給大家介紹了關(guān)于Vue中判斷語(yǔ)句與循環(huán)語(yǔ)句基礎(chǔ)用法及v-if和v-for注意事項(xiàng)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Vue中構(gòu)造數(shù)組數(shù)據(jù)之map和forEach方法實(shí)現(xiàn)
數(shù)組操作是前端最重要的數(shù)據(jù)操作,構(gòu)造數(shù)組數(shù)據(jù),又是數(shù)組操作中很常見(jiàn)的,本文將梳理下map和forEach方法在Vue項(xiàng)目中的使用,感興趣的朋友跟隨小編一起看看吧2022-09-09vue.js實(shí)現(xiàn)動(dòng)態(tài)面包屑
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)動(dòng)態(tài)面包屑,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04使用Vue組件實(shí)現(xiàn)一個(gè)簡(jiǎn)單彈窗效果
這篇文章主要介紹了使用Vue組件實(shí)現(xiàn)一個(gè)簡(jiǎn)單彈窗效果,本文主要內(nèi)容會(huì)涉及到彈窗遮罩的實(shí)現(xiàn), slot 插槽的使用方式,props 、 $emit 傳參,具體組件代碼也傳上去了。需要的朋友可以參考下2018-04-04vue3+vite項(xiàng)目跨域配置踩坑實(shí)戰(zhàn)篇
vue3是一個(gè)流行的前端框架,vite是一個(gè)快速的構(gòu)建工具,下面這篇文章主要給大家介紹了關(guān)于vue3+vite項(xiàng)目跨域配置踩坑實(shí)戰(zhàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05vue中watch監(jiān)聽(tīng)對(duì)象中某個(gè)屬性的方法
watch 的用法有個(gè)特點(diǎn),就是當(dāng)值第一次綁定的時(shí)候,不會(huì)執(zhí)行監(jiān)聽(tīng)函數(shù),只有值發(fā)生改變才會(huì)執(zhí)行,如果我們需要在最初綁定值得時(shí)候也執(zhí)行函數(shù),就需要用到 immediate 屬性,這篇文章主要介紹了vue中watch監(jiān)聽(tīng)對(duì)象中某個(gè)屬性的方法,需要的朋友可以參考下2023-04-04