欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue 通過下拉框組件學(xué)習(xí)vue中的父子通訊

 更新時間:2017年12月19日 09:25:27   作者:深谷逸風(fēng)  
這篇文章主要介紹了vue 通過下拉框組件學(xué)習(xí)vue中的父子通訊的相關(guān)知識,文中涉及到了父組件,子組件的實(shí)現(xiàn)代碼,需要的朋友可以參考下

如果說vue組件化開發(fā)中第一步應(yīng)該了解的是什么的話,那無疑是父子組件之間是如何實(shí)現(xiàn)通訊的(說白了就是父子組件中數(shù)據(jù)是如何傳遞的),只有理解了這一步,才能更好的開發(fā)組件

這里先提出兩個關(guān)鍵詞: props 與 emit :

寫這個組件之前,先看看效果圖:

 

組件開發(fā)分析:

既然是組件:

  • 首先組件內(nèi)部數(shù)據(jù)內(nèi)容肯定是可變的(如上圖中的"按時間排序"之類的),這必須由父組件傳入(即父組件如何將數(shù)據(jù)傳個父組件);
  • 在選擇了內(nèi)容之后,如何將數(shù)據(jù)傳出來(即子組件如何將數(shù)據(jù)傳給父組件)

先寫結(jié)構(gòu):

父組件

<!--下拉框父組件-->
<template>
 <div id="app">
  <oSelect @changeOption="onChangeOption" :selectData="selectData"></oSelect>
  <!--
  selectData: 傳入父組件需要傳入的數(shù)據(jù);格式:childDataName="parentDataName";
  onChangeOption: 子組件觸發(fā)的事件名,通過觸發(fā)一個事件從而來接收子組件的傳過來的數(shù)據(jù)
  格式:@childEventName="parentEventName"
  注:可寫多個
  -->
 </div>
</template>
<script>
import oSelect from "@/components/select.vue"; //引入組件
export default{
 name: 'App',
 data(){
  return {
   selectData: {
    defaultIndex: 0, //默認(rèn)選中的是第幾個
    selectStatus: false, // 通過selectStatus來控制下拉框的顯示/隱藏
    selectOptions: [ // 下拉框中的數(shù)據(jù) name這樣的參數(shù),看項目是否有需求,可自行修改
     {
      name: 'time',
      context: '按時間排序'
     },
     {
      name: 'view',
      context: '按瀏覽量排序'
     },
     {
      name: 'like',
      context: '按點(diǎn)贊數(shù)排序'
     },
     {
      name: 'reply',
      context: '按回復(fù)數(shù)排序'
     },
     {
      name: 'reward',
      context: '按打賞數(shù)排序'
     }
    ]
   }
  }
 },
 methods:{
  onChangeOption(index){
  //子組件通過一個事件來觸發(fā)onChangeOption方法,從而傳遞一系列參數(shù),這里的index就是傳過來的
   this.selectData.defaultIndex = index;
  //觸發(fā)過后,動態(tài)改變了需要值 
  }
 },
 components: {
  oSelect,
  //注冊組件
 }
}
</script>

子組件

<template>
<!-- 下拉框組件html結(jié)構(gòu)(子組件) -->
<div class="select-box" @click="changeStatus">
<!-- changeStatus事件: 點(diǎn)擊實(shí)現(xiàn)下拉框的顯示和隱藏 -->
<h3 class="select-title"
 :name="selectData.selectOptions[selectData.defaultIndex].name"
 :class="{'select-title-active': selectData.selectStatus}"> 
 <!--屬性name class的動態(tài)綁定-->
 {{ selectData.selectOptions[selectData.defaultIndex].context }} 
 <!--這里主要綁定選擇的數(shù)據(jù)-->
</h3>
<transition name="slide-down">
<!--transition 實(shí)現(xiàn)下拉列表顯示隱藏時的動畫-->
<ul class="select-options" v-show="selectData.selectStatus">
 <li class="select-option-item" 
  v-for="(item,index) in selectData.selectOptions"
  @click="EmitchangeOption(index)"
  :class="{'select-option-active':selectData.defaultIndex===index}">
  <!--
   v-for:循環(huán)數(shù)據(jù)渲染下拉列表
   EmitchangeOption:點(diǎn)擊下拉列表事件
   class:動態(tài)綁定被選中的數(shù)據(jù)
  -->
  {{ selectData.selectOptions[index].context }}
  
 </li>
 <div class="arrow-top"></div>
</ul> 
</transition> 
</div> 
</template>
<script>
export default{
 name: 'oSelect', //建議大家都寫上這個,有利于我們知道這個組件叫什么名字
 //通過props來接收父組件傳過來的數(shù)據(jù)
 props:{
  selectData: {
  type: Object //規(guī)定傳過來的數(shù)據(jù)為對象,否則就會報錯(其實(shí)這樣寫就是規(guī)避錯誤和良好的習(xí)慣)
  }
 },
 methods:{
  EmitchangeOption(index){
  this.$emit('changeOption',index);
   // 通過點(diǎn)擊事件觸發(fā)EmitchangeOption函數(shù),傳入當(dāng)前點(diǎn)擊下拉列表中的索引值index
   // 下拉框通過emit方法觸發(fā)父組件中changeOption函數(shù),動態(tài)傳給父組件需要的數(shù)據(jù),這里為索引值
  },
  changeStatus(){
   // 通過changeStatus事件動態(tài)改變selectStatus的值,從而控制下拉框的顯示隱藏
  this.selectData.selectStatus = !this.selectData.selectStatus
  }
 }
}
</script>

總結(jié)

  • 從以上的示例可以看出來,父組件傳入數(shù)據(jù),需要在父組件中線綁定一個屬性,掛載需要傳入的數(shù)據(jù);
  • 子組件接收父組件的數(shù)據(jù)通過 props 方法來接收;
  • 子組件傳遞數(shù)據(jù)需要使用 emit 方法來綁定父組件中事先設(shè)定好的方法,從而動態(tài)傳遞操作后需要的數(shù)據(jù)

最終效果如下:

 

附上組件中的css,僅供參考:

.select-box{
 position: relative;
 max-width: 250px;
 line-height: 35px;
 margin: 50px auto;
}
.select-title{
 position: relative;
 padding: 0 30px 0 10px;
 border: 1px solid #d8dce5;
 border-radius: 5px;
 transition-duration: 300ms;
 cursor: pointer;
}
.select-title:after{
 content: '';
 position: absolute;
 height: 0;
 width: 0;
 border-top: 6px solid #d8dce5;
 border-left: 6px solid transparent;
 border-right: 6px solid transparent;
 right: 9px;
 top: 0;
 bottom: 0;
 margin: auto;
 transition-duration: 300ms;
 transition-timing-function: ease-in-out;
}
.select-title-active{
 border-color: #409eff;
}
.select-title-active:after{
 transform: rotate(-180deg);
 border-top-color: #409eff;
}
.select-options{
 position: absolute;
 padding:10px 0;
 top: 45px;
 border:1px solid #d8dce5;
 width: 100%;
 border-radius: 5px;
}
.select-option-item{
 padding:0 10px;
 cursor: pointer;
 transition-duration: 300ms;
}
.select-option-item:hover,.select-option-active{
 background: #f1f1f1;
 color: #409eff;
}
<!--箭頭css-->
.arrow-top{
 position: absolute;
 height: 0;
 width: 0;
 top: -7px;
 border-bottom: 7px solid #d8dce5;
 border-left: 7px solid transparent;
 border-right: 7px solid transparent;
 left: 0;
 right: 0;
 margin: auto;
 z-index: 99;
}
.arrow-top:after{
 content: '';
 position: absolute;
 display: block;
 height: 0;
 width: 0;
 border-bottom: 6px solid #fff;
 border-left: 6px solid transparent;
 border-right: 6px solid transparent;
 left: -6px;
 top: 1px;
 z-index: 99;
}
<!--下拉框顯示隱藏動畫-->
.slide-down-enter-active,.slide-down-leave{
 transition: all .3s ease-in-out;
 transform-origin:0 top;
 transform: scaleY(1);
}
.slide-down-enter{
 transform: scaleY(0);
}
.slide-down-leave-active{
 transition: all .3s ease-in-out;
 transform-origin:0 top;
 transform: scaleY(0);
}

總結(jié)

以上所述是小編給大家介紹的vue 通過下拉框組件學(xué)習(xí)vue中的父子通訊,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vue+Openlayer中使用select選擇要素的實(shí)現(xiàn)代碼

    Vue+Openlayer中使用select選擇要素的實(shí)現(xiàn)代碼

    本文通過實(shí)例代碼給大家介紹Vue+Openlayer中使用select選擇要素,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-08-08
  • Vue實(shí)現(xiàn)boradcast和dispatch的示例

    Vue實(shí)現(xiàn)boradcast和dispatch的示例

    這篇文章主要介紹了Vue實(shí)現(xiàn)boradcast和dispatch的示例,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-11-11
  • Vue3引入騰訊地圖包含標(biāo)注簡易操作指南

    Vue3引入騰訊地圖包含標(biāo)注簡易操作指南

    這篇文章主要介紹了Vue3引入騰訊地圖的相關(guān)資料,并實(shí)現(xiàn)點(diǎn)擊地圖添加標(biāo)注的功能,示例代碼提供了添加單個或多個標(biāo)注的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • vue項目實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法

    vue項目實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法

    這篇文章主要給大家分享的是vue項目實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法,vue-router是前端開發(fā)中用來實(shí)現(xiàn)路由頁面跳轉(zhuǎn)的一個模塊。下面小編將帶來如何在已經(jīng)創(chuàng)建好的vue-router項目基礎(chǔ)下實(shí)現(xiàn)頁面跳轉(zhuǎn),需要的朋友可以參考一下
    2021-11-11
  • Vue數(shù)據(jù)監(jiān)聽方法watch的使用

    Vue數(shù)據(jù)監(jiān)聽方法watch的使用

    這篇文章主要介紹了Vue數(shù)據(jù)監(jiān)聽方法watch的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • vue封裝一個圖案手勢鎖組件

    vue封裝一個圖案手勢鎖組件

    手勢鎖是常見的一種手機(jī)解鎖方式,本文主要介紹了vue封裝一個圖案手勢鎖組件,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 超全面的vue.js使用總結(jié)

    超全面的vue.js使用總結(jié)

    Vue.js是當(dāng)下很火的一個JavaScript MVVM庫,它是以數(shù)據(jù)驅(qū)動和組件化的思想構(gòu)建的。相比于Angular.js,Vue.js提供了更加簡潔、更易于理解的API,使得我們能夠快速地上手并使用Vue.js。下面這篇文章主要給大家介紹了關(guān)于vue.js使用的相關(guān)總結(jié),需要的朋友可以參考借鑒。
    2017-02-02
  • vue項目打包后上傳至GitHub并實(shí)現(xiàn)github-pages的預(yù)覽

    vue項目打包后上傳至GitHub并實(shí)現(xiàn)github-pages的預(yù)覽

    這篇文章主要介紹了vue項目打包后上傳至GitHub并實(shí)現(xiàn)github-pages的預(yù)覽,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • element-ui如何在table中使用tooltip

    element-ui如何在table中使用tooltip

    這篇文章主要介紹了element-ui如何在table中使用tooltip問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實(shí)現(xiàn)

    搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實(shí)現(xiàn)

    這篇文章主要介紹了搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評論