亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

在vue3.0中封裝button使用slot組件

 更新時間:2022年05月27日 15:48:00   作者:周家大小姐.  
這篇文章主要介紹了在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:{//傳值類型,默認(rèn)值為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 個 判斷,而且如果該組件還將被別的頁面引用到,那就得還得復(fù)制一遍。代碼一點也不優(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 || '默認(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>封裝一個 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)文章

  • vue-cli實現(xiàn)異步請求返回mock模擬數(shù)據(jù)

    vue-cli實現(xiàn)異步請求返回mock模擬數(shù)據(jù)

    網(wǎng)上有不少使用mockjs模擬數(shù)據(jù)的文章,但基本都是本地攔截請求返回數(shù)據(jù),本文主要介紹了vue-cli實現(xiàn)異步請求返回mock模擬數(shù)據(jù),文中根據(jù)實例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vue實現(xiàn)數(shù)據(jù)篩選與搜索功能的示例代碼

    Vue實現(xiàn)數(shù)據(jù)篩選與搜索功能的示例代碼

    在許多Web應(yīng)用程序中,數(shù)據(jù)篩選和搜索是關(guān)鍵的用戶體驗功能,本文將深入探討在Vue中如何進行數(shù)據(jù)篩選與搜索的實現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • 如何在vue3.0+中使用tinymce及實現(xiàn)多圖上傳文件上傳公式編輯功能

    如何在vue3.0+中使用tinymce及實現(xiàn)多圖上傳文件上傳公式編輯功能

    本文給大家分享tinymce編輯器如何在vue3.0+中使用tinymce及實現(xiàn)多圖上傳文件上傳公式編輯功能,tinymce安裝方法文中也給大家詳細(xì)介紹了,對vue tinymce實現(xiàn)上傳公式編輯相關(guān)知識感興趣的朋友跟隨小編一起學(xué)習(xí)下吧
    2021-05-05
  • 詳解vue+nodejs獲取多個表數(shù)據(jù)的方法

    詳解vue+nodejs獲取多個表數(shù)據(jù)的方法

    這篇文章主要為大家介紹了vue+nodejs獲取多個表數(shù)據(jù)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Vue中的table表單切換實現(xiàn)效果

    Vue中的table表單切換實現(xiàn)效果

    這篇文章主要介紹了Vue中的table表單切換實現(xiàn)效果,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue導(dǎo)入excel文件,vue導(dǎo)入多個sheets的方式

    vue導(dǎo)入excel文件,vue導(dǎo)入多個sheets的方式

    這篇文章主要介紹了vue導(dǎo)入excel文件,vue導(dǎo)入多個sheets的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 手把手教你vue-cli單頁到多頁應(yīng)用的方法

    手把手教你vue-cli單頁到多頁應(yīng)用的方法

    本篇文章主要介紹了手把手教你vue-cli單頁到多頁應(yīng)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Vue中列表渲染指令v-for的基本用法詳解

    Vue中列表渲染指令v-for的基本用法詳解

    v-for指令是在模板編譯的代碼生成階段實現(xiàn)的,當(dāng)遍歷數(shù)組或?qū)ο髸r需要使用列表渲染指令v-for。本文主要為大家介紹了v-for指令的基本用法,希望對大家有所幫助
    2023-04-04
  • Vue 解決通過this.$refs來獲取DOM或者組件報錯問題

    Vue 解決通過this.$refs來獲取DOM或者組件報錯問題

    這篇文章主要介紹了Vue 解決通過this.$refs來獲取DOM或者組件報錯問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法

    vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法

    這篇文章主要介紹了vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-12-12

最新評論