【Vue3+Vite+TS】3.0 組件二:圖標(biāo)選擇器

必備UI組件

圖標(biāo)選擇器將用到以下幾個(gè)組件:
Icon 圖標(biāo)
Button 按鈕
Message 消息提示
Dialog 對(duì)話框

組件設(shè)計(jì)

修改路由src\router\index.ts

/*
 * @Author: bobokaka
 * @Date: 2021-12-19 11:26:38
 * @LastEditTime: 2021-12-20 21:17:12
 * @LastEditors: bobokaka
 * @Description: 路由
 * @FilePath: \vue3-element-ui-baseline\src\router\index.ts
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

import Home from '../views/Home/index.vue'
import Container from '../components/baseline/container/src/index.vue'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        component: Container,
        children: [
            {
                path: '/',
                component: Home,
            },
            {
                path: '/chooseIcon',
                component: () => import('../views/chooseIcon/index.vue'),
            },
        ],
    },
]

const router = createRouter({
    routes,
    history: createWebHistory(),
})
export default router

新建src\views\chooseIcon\index.vue

<!--
 * @Author: bobokaka
 * @Date: 2021-12-20 20:29:00
 * @LastEditTime: 2021-12-20 21:03:47
 * @LastEditors: bobokaka
 * @Description: 圖標(biāo)選擇器
 * @FilePath: \vue3-element-ui-baseline\src\views\chooseIcon\index.vue
-->
<template>
    <div>
        <choose-icon title="選擇圖標(biāo)" v-model:visiable="visiable">
            選擇圖標(biāo)
        </choose-icon>
    </div>
</template>
<script lang="ts" setup>
import ChooseIcon from '../../components/baseline/chooseIcon/src/index.vue'
import { ref } from 'vue'
const visiable = ref(false)
</script>
<style lang="scss" scoped></style>

新增src\components\baseline\chooseIcon\src\index.vue

<!--
 * @Author: bobokaka
 * @Date: 2021-12-20 01:41:21
 * @LastEditTime: 2021-12-20 21:15:36
 * @LastEditors: Please set LastEditors
 * @Description: 選擇圖標(biāo)
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\chooseIcon\src\index.vue
-->
<template>
    <el-button @click="handleClick" type="primary">
        <slot></slot>
    </el-button>
    <el-dialog :title="title" v-model="currentVisiable">111</el-dialog>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
const props = defineProps<{
    //彈出框標(biāo)題
    title: string
    //控制彈出框的顯示與隱藏
    visiable: boolean
}>()
const emits = defineEmits(['update:visiable'])

const currentVisiable = ref<boolean>(props.visiable)
const handleClick = () => {
    //需要修改父組件的數(shù)據(jù)
    emits('update:visiable', !props.visiable)
}

//拷貝一份父組件傳遞過來的visiable
//監(jiān)聽visiable的變化

watch(
    () => props.visiable,
    val => {
        currentVisiable.value = val
    }
)
//監(jiān)聽組件內(nèi)部currentVisiable的變化
watch(
    () => currentVisiable.value,
    val => {
        emits('update:visiable', val)
    }
)
</script>
<style lang="scss" scoped></style>

輸入地址http://localhost:8080/chooseIcon

image.png

進(jìn)一步調(diào)整:
利用vue的特性component:動(dòng)態(tài)組件的特性昧识,is參數(shù)傳什么就會(huì)生成一個(gè)對(duì)應(yīng)得標(biāo)簽医咨,比如:is="hhh",就會(huì)生成<hhh></hhh>
修改src\components\baseline\chooseIcon\src\index.vue

<!--
 * @Author: bobokaka
 * @Date: 2021-12-20 01:41:21
 * @LastEditTime: 2021-12-20 21:26:57
 * @LastEditors: Please set LastEditors
 * @Description: 選擇圖標(biāo)
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\chooseIcon\src\index.vue
-->
<template>
    <el-button @click="handleClick" type="primary">
        <slot></slot>
    </el-button>
    <el-dialog :title="title" v-model="currentVisiable">
        <div class="bs-wrapper__container">
            <div v-for="(item, index) in Object.keys(ElIcons)" :key="index">
                <div>
                    <component :is="`el-icon-${toLine(item)}`"></component>
                </div>
                <div>{{ item }}</div>
            </div>
        </div>
    </el-dialog>
</template>

<script lang="ts" setup>
import * as ElIcons from '@element-plus/icons-vue'
import { toLine } from '../../../../utils'
import { ref, watch } from 'vue'
const props = defineProps<{
    //彈出框標(biāo)題
    title: string
    //控制彈出框的顯示與隱藏
    visiable: boolean
}>()
const emits = defineEmits(['update:visiable'])

const currentVisiable = ref<boolean>(props.visiable)
const handleClick = () => {
    //需要修改父組件的數(shù)據(jù)
    emits('update:visiable', !props.visiable)
}

//拷貝一份父組件傳遞過來的visiable
//監(jiān)聽visiable的變化

watch(
    () => props.visiable,
    val => {
        currentVisiable.value = val
    }
)
//監(jiān)聽組件內(nèi)部currentVisiable的變化
watch(
    () => currentVisiable.value,
    val => {
        emits('update:visiable', val)
    }
)
</script>
......

效果如下:


image.png

樣式優(yōu)化

優(yōu)化css屬性:
修改src\components\baseline\chooseIcon\src\index.vue

<!--
 * @Author: bobokaka
 * @Date: 2021-12-20 01:41:21
 * @LastEditTime: 2021-12-20 21:44:19
 * @LastEditors: Please set LastEditors
 * @Description: 選擇圖標(biāo)
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\chooseIcon\src\index.vue
-->
<template>
    <el-button @click="handleClick" type="primary">
        <slot></slot>
    </el-button>
    <el-dialog :title="title" v-model="currentVisiable">
        <div class="bs-wrapper__container">
            <div
                v-for="(item, index) in Object.keys(ElIcons)"
                :key="index"
                class="bs-wrapper__container__item"
            >
                <div>
                    <component :is="`el-icon-${toLine(item)}`"></component>
                </div>
                <div class="bs-wrapper__container__item__title">{{ item }}</div>
            </div>
        </div>
    </el-dialog>
</template>

<script lang="ts" setup>
import * as ElIcons from '@element-plus/icons-vue'
import { toLine } from '../../../../utils'
import { ref, watch } from 'vue'
const props = defineProps<{
    //彈出框標(biāo)題
    title: string
    //控制彈出框的顯示與隱藏
    visiable: boolean
}>()
const emits = defineEmits(['update:visiable'])

const currentVisiable = ref<boolean>(props.visiable)
const handleClick = () => {
    //需要修改父組件的數(shù)據(jù)
    emits('update:visiable', !props.visiable)
}

//拷貝一份父組件傳遞過來的visiable
//監(jiān)聽visiable的變化

watch(
    () => props.visiable,
    val => {
        currentVisiable.value = val
    }
)
//監(jiān)聽組件內(nèi)部currentVisiable的變化
watch(
    () => currentVisiable.value,
    val => {
        emits('update:visiable', val)
    }
)
</script>
<style lang="scss" scoped>
.bs-wrapper {
    &__container {
        display: flex;
        align-items: center; //垂直方向居中
        flex-wrap: wrap;
        &__item {
            width: 25%; //5個(gè)一行
            display: flex;
            flex-direction: column; //靈活的項(xiàng)目將垂直顯示,正如一個(gè)列一樣布近。
            align-items: center; //垂直方向居中
            justify-content: center; //水平方向居中
            margin-bottom: 0.15rem;
            cursor: pointer;
            font-size: 16px;
            height: 0.7rem;
            &__title {
                flex: 1; //獨(dú)占一份
            }
        }
    }
}

svg {
    width: 0.4rem;
    height: 0.4rem;
}
</style>

image.png

現(xiàn)在的效果是可以不停向下滾動(dòng)炎辨,優(yōu)化成頁內(nèi)滾動(dòng)捕透,方式如下:
新建src\style\ui.scss

//修改組件庫內(nèi)部的樣式
//1.需要自定義一個(gè)類名空間
//2.瀏覽器中調(diào)試樣式
//3.調(diào)試好的類名放在這個(gè)類名中
//4.在App.vue里面引入這個(gè)文件
//5.在組件內(nèi)需要改樣式的元素的父元素加上這個(gè)類名
.bk--choose-icon-dialog-body-height {
    .el-dialog__body {
        height: 5rem;
        overflow: scroll;
    }
}

修改src\style\index.scss

@import './base.scss';
@import './ui.scss'; 

修改src\components\baseline\chooseIcon\src\index.vue

<!--
 * @Author: bobokaka
 * @Date: 2021-12-20 01:41:21
 * @LastEditTime: 2021-12-20 22:58:14
 * @LastEditors: Please set LastEditors
 * @Description: 選擇圖標(biāo)
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\chooseIcon\src\index.vue
-->
<template>
    <div>
        <div>
            <el-button @click="handleClick" type="primary">
                <slot></slot>
            </el-button>
        </div>
        <div class="bk--choose-icon-dialog-body-height">
            <el-dialog :title="title" v-model="currentVisiable">
                <div class="bs-wrapper__container">
                    <div
                        v-for="(item, index) in Object.keys(ElIcons)"
                        :key="index"
                        class="bs-wrapper__container__item"
                    >
                        <div>
                            <component
                                :is="`el-icon-${toLine(item)}`"
                            ></component>
                        </div>
                        <div class="bs-wrapper__container__item__title">
                            {{ item }}
                        </div>
                    </div>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script lang="ts" setup>
import * as ElIcons from '@element-plus/icons-vue'
import { toLine } from '../../../../utils'
import { ref, watch } from 'vue'
const props = defineProps<{
    //彈出框標(biāo)題
    title: string
    //控制彈出框的顯示與隱藏
    visiable: boolean
}>()
const emits = defineEmits(['update:visiable'])

const currentVisiable = ref<boolean>(props.visiable)
const handleClick = () => {
    //需要修改父組件的數(shù)據(jù)
    emits('update:visiable', !props.visiable)
}

//拷貝一份父組件傳遞過來的visiable
//監(jiān)聽visiable的變化

watch(
    () => props.visiable,
    val => {
        currentVisiable.value = val
    }
)
//監(jiān)聽組件內(nèi)部currentVisiable的變化
watch(
    () => currentVisiable.value,
    val => {
        emits('update:visiable', val)
    }
)
</script>
<style lang="scss" scoped>
.bs-wrapper {
    &__container {
        display: flex;
        align-items: center; //垂直方向居中
        flex-wrap: wrap;
        &__item {
            width: 25%; //5個(gè)一行
            display: flex;
            flex-direction: column; //靈活的項(xiàng)目將垂直顯示,正如一個(gè)列一樣碴萧。
            align-items: center; //垂直方向居中
            justify-content: center; //水平方向居中
            margin-bottom: 0.15rem;
            cursor: pointer;
            font-size: 16px;
            height: 0.7rem;
            &__title {
                flex: 1; //獨(dú)占一份
            }
        }
    }
}

svg {
    width: 0.4rem;
    height: 0.4rem;
}
</style>

效果如下:


image.png

這里/style/index.scss在src\main.ts引入

import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as Icons from '@element-plus/icons-vue'
import { toLine } from './utils'

import './style/index.scss'

const app = createApp(App)

//全局組件注冊(cè)乙嘀,犧牲一些性能,但方便使用
//封裝成el-icon-xxx
for (let i in Icons) {
    // console.log(`i:`, i)
    // console.log('Icons[i]', (Icons as any)[i])
    app.component(`el-icon-${toLine(i)}`, (Icons as any)[i])
}

app.use(router)
app.use(ElementPlus)
app.mount('#app')

滾動(dòng)條還可以再優(yōu)化一下:
修改src\style\ui.scss

//修改組件庫內(nèi)部的樣式
//1.需要自定義一個(gè)類名空間
//2.瀏覽器中調(diào)試樣式
//3.調(diào)試好的類名放在這個(gè)類名中
//4.在App.vue里面引入這個(gè)文件
//5.在組件內(nèi)需要改樣式的元素的父元素加上這個(gè)類名
.bk--choose-icon-dialog-body-height {
    .el-dialog__body {
        height: 5rem;
        overflow-y: scroll;
        overflow-x: auto;
    }
}

image.png

利用hooks函數(shù)實(shí)現(xiàn)復(fù)制

hooks函數(shù)都是以u(píng)se開頭破喻。
src\hooks\useCopy\index.ts


import { ElMessage } from 'element-plus'
export const useCopy = (text: string) => {
    //創(chuàng)建一個(gè)input輸入框
    let input = document.createElement('input')
    //給輸入框value賦值
    input.value = text
    //追加到body中
    document.body.appendChild(input)
    //選擇輸入框的操作
    input.select()
    //執(zhí)行復(fù)制操作
    document.execCommand('Copy')
    //刪除input輸入框的dom節(jié)點(diǎn)
    document.body.removeChild(input)
    //提示用戶
    ElMessage.success('復(fù)制成功')
}

修改src\components\baseline\chooseIcon\src\index.vue

<!--
 * @Author: bobokaka
 * @Date: 2021-12-20 01:41:21
 * @LastEditTime: 2021-12-21 19:33:02
 * @LastEditors: Please set LastEditors
 * @Description: 選擇圖標(biāo)
 * @FilePath: \vue3-element-ui-baseline\src\components\baseline\chooseIcon\src\index.vue
-->
<template>
    <div>
        <div>
            <el-button @click="handleClick" type="primary">
                <slot></slot>
            </el-button>
        </div>
        <div class="bk--choose-icon-dialog-body-height">
            <el-dialog :title="title" v-model="currentVisiable">
                <div class="bs-wrapper__container">
                    <div
                        v-for="(item, index) in Object.keys(ElIcons)"
                        :key="index"
                        class="bs-wrapper__container__item"
                        @click="handleClickItem(item)"
                    >
                        <div>
                            <component
                                :is="`el-icon-${toLine(item)}`"
                            ></component>
                        </div>
                        <div class="bs-wrapper__container__item__title">
                            {{ item }}
                        </div>
                    </div>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script lang="ts" setup>
import * as ElIcons from '@element-plus/icons-vue'
import { toLine } from '../../../../utils'
import { ref, watch } from 'vue'
import { useCopy } from '../../../../hooks/useCopy'
const props = defineProps<{
    //彈出框標(biāo)題
    title: string
    //控制彈出框的顯示與隱藏
    visiable: boolean
}>()
const emits = defineEmits(['update:visiable'])

const currentVisiable = ref<boolean>(props.visiable)

//點(diǎn)擊按鈕顯示彈出框
const handleClick = () => {
    //需要修改父組件的數(shù)據(jù)
    emits('update:visiable', !props.visiable)
}

//點(diǎn)擊圖標(biāo)
const handleClickItem = (item: string) => {
    let text = `<el-icon-${toLine(item)} />`
    console.log(`text:`, text)
    useCopy(text)
    //關(guān)閉彈框
    currentVisiable.value = false
}

//拷貝一份父組件傳遞過來的visiable
//監(jiān)聽visiable的變化

watch(
    () => props.visiable,
    val => {
        currentVisiable.value = val
    }
)
//監(jiān)聽組件內(nèi)部currentVisiable的變化
watch(
    () => currentVisiable.value,
    val => {
        emits('update:visiable', val)
    }
)
</script>
<style lang="scss" scoped>
.bs-wrapper {
    &__container {
        display: flex;
        align-items: center; //垂直方向居中
        flex-wrap: wrap;
        &__item {
            width: 25%; //5個(gè)一行
            display: flex;
            flex-direction: column; //靈活的項(xiàng)目將垂直顯示虎谢,正如一個(gè)列一樣。
            align-items: center; //垂直方向居中
            justify-content: center; //水平方向居中
            margin-bottom: 0.15rem;
            cursor: pointer;
            font-size: 16px;
            height: 0.7rem;
            &__title {
                flex: 1; //獨(dú)占一份
            }
        }
    }
}

svg {
    width: 0.4rem;
    height: 0.4rem;
}
</style>

功能開發(fā)完畢曹质。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末婴噩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子羽德,更是在濱河造成了極大的恐慌几莽,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宅静,死亡現(xiàn)場(chǎng)離奇詭異章蚣,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)姨夹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門纤垂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人匀伏,你說我怎么就攤上這事洒忧。” “怎么了够颠?”我有些...
    開封第一講書人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵熙侍,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng)蛉抓,這世上最難降的妖魔是什么庆尘? 我笑而不...
    開封第一講書人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮巷送,結(jié)果婚禮上驶忌,老公的妹妹穿的比我還像新娘。我一直安慰自己笑跛,他們只是感情好付魔,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著飞蹂,像睡著了一般几苍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上陈哑,一...
    開封第一講書人閱讀 49,007評(píng)論 1 284
  • 那天妻坝,我揣著相機(jī)與錄音,去河邊找鬼惊窖。 笑死刽宪,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的界酒。 我是一名探鬼主播圣拄,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼盾计!你這毒婦竟也來了售担?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤署辉,失蹤者是張志新(化名)和其女友劉穎族铆,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哭尝,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡哥攘,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了材鹦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逝淹。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖桶唐,靈堂內(nèi)的尸體忽然破棺而出栅葡,到底是詐尸還是另有隱情,我是刑警寧澤尤泽,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布欣簇,位于F島的核電站规脸,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏熊咽。R本人自食惡果不足惜莫鸭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望横殴。 院中可真熱鬧被因,春花似錦、人聲如沸衫仑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惑畴。三九已至蛋欣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間如贷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工到踏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留杠袱,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓窝稿,卻偏偏與公主長(zhǎng)得像楣富,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子伴榔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345

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