在vue3.0中封裝button使用slot組件
封裝button使用slot組件
需求
同一個button在不同頁面使用,只有文字不一樣;有的內(nèi)容為登錄有的為注冊
下面我們自封一個button組件
子組件
<template>
<!-- :type="type" 為按鈕類型 :disabled="disabled" 為判斷他為false還是ture ?
? ? {'is-disabled':disabled} 如果為true就可以有is-disabled的樣式
? ? @click="$emit('click')" 傳入一個cklick點擊事件
-->
? ? <button?
? ? ? ? class="y-button" ? ?
? ? ? ? :class="{'is-disabled':disabled}"
? ? ? ? :type="type"
? ? ? ? :disabled="disabled"
? ? ? ? @click="$emit('click')"
? ? >
? ? ? ? <slot>
? ? ? ? ? ? <!-- 這是一個插槽在父組件中可以放入登錄或都注冊的文本字段 -->
? ? ? ? </slot>
? ? </button>
</template><script>
export default {
? ? name:'ybutton',
? ? props:{//傳值去到父組件?
? ? ? ? type:String,
? ? ? ? disable:{//傳值類型,默認值為false
? ? ? ? ? ? type:Boolean,
? ? ? ? ? ? default:false
? ? ? ? }
? ? }
}
</script><style scoped>
/* 獲取焦點的時候和hover的時候改變顏色 */
.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:{//注冊組件
Ybutton
},
computed:{//監(jiān)聽子組件的disabled用于啟用或禁用按鈕
isDisabled(){
if(this.user.email){
// 如果input框有值就讓disabled為false 不禁用
return false;
}else{
return true;
}
}
},
methods:{
loginClick(){
// 實現(xiàn)登錄,存儲token
this.$axios.post('/api/users/login',this.user).then(res =>{
// res 結(jié)果用會返回token 我們可以用解構(gòu)模式給他存儲
const { token } = res.data;
// 存儲ls
localStorage.setItem('wxToken',token);
//存儲之后頁面進行主頁跳轉(zhuǎn)
this.$router.push('/')
})
}
}
}
</script>vue帶你封裝一個button
作為一個后端程序員偶爾搞搞前端,對我自己來說是打開新的領(lǐng)域,提高自己的競爭力,說實話搞前端和搞后端的思維方式是完全不同的,注重點也是非常不同的,話說今天寶寶我農(nóng)歷生日哈哈哈哈,開心就寫幾篇放縱一下。
使用 Vue-cli 創(chuàng)建一個 HelloWorld 項目即可作為起始腳手架。
創(chuàng)建一個 ShowButton.vue 的組件
<template>
<div>
<h1>封裝一個 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 表達式做邏輯判斷,但是如果有 10 個按鈕,那么就需要寫 10 個 判斷,而且如果該組件還將被別的頁面引用到,那就得還得復制一遍。代碼一點也不優(yōu)雅呀。
我們借助于 VUE 給我們提供的 render 函數(shù)解決這個問題。
新建一個 Button.vue
<script>
export default {
? ? props:{
? ? ? ? type:{
? ? ? ? ? ? type:String,
? ? ? ? ? ? default:'normal'
? ? ? ? },
? ? ? ? text:{
? ? ? ? ? ? type:String,
? ? ? ? ? ? default:'button'
? ? ? ? }
? ? },
? ? render(h){
? ? ? ? /**
? ? ? ? ?* h 是 createElement 的另一個名稱, 接受 2 個參數(shù),具體可看 vue 文檔
? ? ? ? ?* 1 - 元素
? ? ? ? ?* 2 - 選項
? ? ? ? ?*/
? ? ? ? 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 || '默認'
? ? ? ? ? ? },
? ? ? ? ? ? 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>封裝一個 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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決axios發(fā)送post請求返回400狀態(tài)碼的問題
今天小編就為大家分享一篇解決axios發(fā)送post請求返回400狀態(tài)碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue中利用simplemde實現(xiàn)markdown編輯器(增加圖片上傳功能)
這篇文章主要介紹了vue中利用simplemde實現(xiàn)markdown編輯器(增加圖片上傳功能),本文通過實例代碼相結(jié)合的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
使vue實現(xiàn)jQuery調(diào)用的兩種方法
這篇文章主要介紹了使vue實現(xiàn)jQuery調(diào)用的兩種方法 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

