在vue3.0中封裝button使用slot組件
封裝button使用slot組件
需求
同一個(gè)button在不同頁(yè)面使用,只有文字不一樣;有的內(nèi)容為登錄有的為注冊(cè)
下面我們自封一個(gè)button組件
子組件
<template> <!-- :type="type" 為按鈕類(lèi)型 :disabled="disabled" 為判斷他為false還是ture ? ? ? {'is-disabled':disabled} 如果為true就可以有is-disabled的樣式 ? ? @click="$emit('click')" 傳入一個(gè)cklick點(diǎn)擊事件 --> ? ? <button? ? ? ? ? class="y-button" ? ? ? ? ? ? :class="{'is-disabled':disabled}" ? ? ? ? :type="type" ? ? ? ? :disabled="disabled" ? ? ? ? @click="$emit('click')" ? ? > ? ? ? ? <slot> ? ? ? ? ? ? <!-- 這是一個(gè)插槽在父組件中可以放入登錄或都注冊(cè)的文本字段 --> ? ? ? ? </slot> ? ? </button> </template>
<script> export default { ? ? name:'ybutton', ? ? props:{//傳值去到父組件? ? ? ? ? type:String, ? ? ? ? disable:{//傳值類(lèi)型,默認(rèn)值為false ? ? ? ? ? ? type:Boolean, ? ? ? ? ? ? default:false ? ? ? ? } ? ? } } </script>
<style scoped> /* 獲取焦點(diǎn)的時(shí)候和hover的時(shí)候改變顏色 */ .is-disabled:focus, .is-disabled:hover{ ? ? background: blue; ? ? color:white; ? } </style>
父組件引用
<template> <div> <input type="text" v-model="use.email"> <div class="btn_wrap"> <Ybutton :disabled="isDisabled" @click="loginClick">登錄</Ybutton> </div> </div> </template>
<script> // 引入button組件 import Ybutton from "./Ybutton" export default { data(){ return{ user:{ email:'' } } }, components:{//注冊(cè)組件 Ybutton }, computed:{//監(jiān)聽(tīng)子組件的disabled用于啟用或禁用按鈕 isDisabled(){ if(this.user.email){ // 如果input框有值就讓disabled為false 不禁用 return false; }else{ return true; } } }, methods:{ loginClick(){ // 實(shí)現(xiàn)登錄,存儲(chǔ)token this.$axios.post('/api/users/login',this.user).then(res =>{ // res 結(jié)果用會(huì)返回token 我們可以用解構(gòu)模式給他存儲(chǔ) const { token } = res.data; // 存儲(chǔ)ls localStorage.setItem('wxToken',token); //存儲(chǔ)之后頁(yè)面進(jìn)行主頁(yè)跳轉(zhuǎn) this.$router.push('/') }) } } } </script>
vue帶你封裝一個(gè)button
作為一個(gè)后端程序員偶爾搞搞前端,對(duì)我自己來(lái)說(shuō)是打開(kāi)新的領(lǐng)域,提高自己的競(jìng)爭(zhēng)力,說(shuō)實(shí)話搞前端和搞后端的思維方式是完全不同的,注重點(diǎn)也是非常不同的,話說(shuō)今天寶寶我農(nóng)歷生日哈哈哈哈,開(kāi)心就寫(xiě)幾篇放縱一下。
使用 Vue-cli 創(chuàng)建一個(gè) HelloWorld 項(xiàng)目即可作為起始腳手架。
創(chuàng)建一個(gè) ShowButton.vue 的組件
<template> <div> <h1>封裝一個(gè) button</h1> <div v-if="value === 1"> <button @click="buttonClick()">button1</button> </div> <div v-else-if="value === 2"> <button @click="buttonClick()">button2</button> </div> <div v-else> <button @click="buttonClick()">button3</button> </div> </div> </template>
<script> export default { name: "ShowButton", data() { return { value: 2 }; }, methods: { buttonClick() { console.log("value" + this.value); } } }; </script> <style> </style>
這里用了vue 里的 v-if 表達(dá)式做邏輯判斷,但是如果有 10 個(gè)按鈕,那么就需要寫(xiě) 10 個(gè) 判斷,而且如果該組件還將被別的頁(yè)面引用到,那就得還得復(fù)制一遍。代碼一點(diǎn)也不優(yōu)雅呀。
我們借助于 VUE 給我們提供的 render 函數(shù)解決這個(gè)問(wèn)題。
新建一個(gè) Button.vue
<script> export default { ? ? props:{ ? ? ? ? type:{ ? ? ? ? ? ? type:String, ? ? ? ? ? ? default:'normal' ? ? ? ? }, ? ? ? ? text:{ ? ? ? ? ? ? type:String, ? ? ? ? ? ? default:'button' ? ? ? ? } ? ? }, ? ? render(h){ ? ? ? ? /** ? ? ? ? ?* h 是 createElement 的另一個(gè)名稱, 接受 2 個(gè)參數(shù),具體可看 vue 文檔 ? ? ? ? ?* 1 - 元素 ? ? ? ? ?* 2 - 選項(xiàng) ? ? ? ? ?*/ ? ? ? ? return h('button',{ ? ? ? ? ? ? class:{ ? ? ? ? ? ? ? ? btn: true, ? ? ? ? ? ? ? ? 'btn-success': this.type === 'success', ? ? ? ? ? ? ? ? 'btn-danger': this.type === 'danger', ? ? ? ? ? ? ? ? 'btn-warning': this.type === 'warning', ? ? ? ? ? ? ? ? 'btn-normal': this.type === 'normal' ? ? ? ? ? ? }, ? ? ? ? ? ? domProps:{ ? ? ? ? ? ? ? ? innerText: this.text || '默認(rèn)' ? ? ? ? ? ? }, ? ? ? ? ? ? on:{ ? ? ? ? ? ? ? ? click: this.handleClick ? ? ? ? ? ? } ? ? ? ? }) ? ? }, ? ? methods:{ ? ? ? ? handleClick(){ ? ? ? ? ? ? this.$emit('myClick') ? ? ? ? } ? ? } } </script>
<style scoped> .btn{ ? ? width: 100px; ? ? height:40px; ? ? line-height:40px; ? ? border:0px; ? ? border-radius:5px; ? ? color:#ffff; } .btn-success{ ? ? background:#2ecc71; } .btn-danger{ ? ? background:#e74c3c; } .btn-warning{ ? ? background:#f39c12; } .btn-normal{ ? ? background:#bdc3c7; } </style>
ShowButton.vue 內(nèi)使用
<template> ? <div> ? ? <h1>封裝一個(gè) button</h1> ? ? <!-- <div v-if="value === 1"> ? ? ? <button @click="buttonClick()">button1</button> ? ? </div> ? ? <div v-else-if="value === 2"> ? ? ? <button @click="buttonClick()">button2</button> ? ? </div> ? ? <div v-else> ? ? ? <button @click="buttonClick()">button3</button> ? ? </div> --> ? ? ?<Button type='success' text='button1' @myClick="buttonClick()"></Button> ? </div> </template>
<script> import Button from "./Button.vue"; export default { ? name: "ShowButton", ? data() { ? ? return { ? ? ? value: 2 ? ? }; ? }, ? components: { ? ? Button ? }, ? methods: { ? ? buttonClick() { ? ? ? console.log("value" + this.value); ? ? } ? } }; </script> <style> </style>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli實(shí)現(xiàn)異步請(qǐng)求返回mock模擬數(shù)據(jù)
網(wǎng)上有不少使用mockjs模擬數(shù)據(jù)的文章,但基本都是本地?cái)r截請(qǐng)求返回?cái)?shù)據(jù),本文主要介紹了vue-cli實(shí)現(xiàn)異步請(qǐng)求返回mock模擬數(shù)據(jù),文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Vue實(shí)現(xiàn)數(shù)據(jù)篩選與搜索功能的示例代碼
在許多Web應(yīng)用程序中,數(shù)據(jù)篩選和搜索是關(guān)鍵的用戶體驗(yàn)功能,本文將深入探討在Vue中如何進(jìn)行數(shù)據(jù)篩選與搜索的實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10如何在vue3.0+中使用tinymce及實(shí)現(xiàn)多圖上傳文件上傳公式編輯功能
本文給大家分享tinymce編輯器如何在vue3.0+中使用tinymce及實(shí)現(xiàn)多圖上傳文件上傳公式編輯功能,tinymce安裝方法文中也給大家詳細(xì)介紹了,對(duì)vue tinymce實(shí)現(xiàn)上傳公式編輯相關(guān)知識(shí)感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2021-05-05詳解vue+nodejs獲取多個(gè)表數(shù)據(jù)的方法
這篇文章主要為大家介紹了vue+nodejs獲取多個(gè)表數(shù)據(jù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12vue導(dǎo)入excel文件,vue導(dǎo)入多個(gè)sheets的方式
這篇文章主要介紹了vue導(dǎo)入excel文件,vue導(dǎo)入多個(gè)sheets的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08手把手教你vue-cli單頁(yè)到多頁(yè)應(yīng)用的方法
本篇文章主要介紹了手把手教你vue-cli單頁(yè)到多頁(yè)應(yīng)用的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Vue 解決通過(guò)this.$refs來(lái)獲取DOM或者組件報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了Vue 解決通過(guò)this.$refs來(lái)獲取DOM或者組件報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法
這篇文章主要介紹了vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12