最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

寫這個(gè)組件之前,先看看效果圖:

 

組件開發(fā)分析:

既然是組件:

  • 首先組件內(nèi)部數(shù)據(jù)內(nèi)容肯定是可變的(如上圖中的"按時(shí)間排序"之類的),這必須由父組件傳入(即父組件如何將數(shù)據(jù)傳個(gè)父組件);
  • 在選擇了內(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ā)一個(gè)事件從而來接收子組件的傳過來的數(shù)據(jù)
  格式:@childEventName="parentEventName"
  注:可寫多個(gè)
  -->
 </div>
</template>
<script>
import oSelect from "@/components/select.vue"; //引入組件
export default{
 name: 'App',
 data(){
  return {
   selectData: {
    defaultIndex: 0, //默認(rèn)選中的是第幾個(gè)
    selectStatus: false, // 通過selectStatus來控制下拉框的顯示/隱藏
    selectOptions: [ // 下拉框中的數(shù)據(jù) name這樣的參數(shù),看項(xiàng)目是否有需求,可自行修改
     {
      name: 'time',
      context: '按時(shí)間排序'
     },
     {
      name: 'view',
      context: '按瀏覽量排序'
     },
     {
      name: 'like',
      context: '按點(diǎn)贊數(shù)排序'
     },
     {
      name: 'reply',
      context: '按回復(fù)數(shù)排序'
     },
     {
      name: 'reward',
      context: '按打賞數(shù)排序'
     }
    ]
   }
  }
 },
 methods:{
  onChangeOption(index){
  //子組件通過一個(gè)事件來觸發(fā)onChangeOption方法,從而傳遞一系列參數(shù),這里的index就是傳過來的
   this.selectData.defaultIndex = index;
  //觸發(fā)過后,動(dòng)態(tài)改變了需要值 
  }
 },
 components: {
  oSelect,
  //注冊(cè)組件
 }
}
</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的動(dòng)態(tài)綁定-->
 {{ selectData.selectOptions[selectData.defaultIndex].context }} 
 <!--這里主要綁定選擇的數(shù)據(jù)-->
</h3>
<transition name="slide-down">
<!--transition 實(shí)現(xiàn)下拉列表顯示隱藏時(shí)的動(dòng)畫-->
<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:動(dòng)態(tài)綁定被選中的數(shù)據(jù)
  -->
  {{ selectData.selectOptions[index].context }}
  
 </li>
 <div class="arrow-top"></div>
</ul> 
</transition> 
</div> 
</template>
<script>
export default{
 name: 'oSelect', //建議大家都寫上這個(gè),有利于我們知道這個(gè)組件叫什么名字
 //通過props來接收父組件傳過來的數(shù)據(jù)
 props:{
  selectData: {
  type: Object //規(guī)定傳過來的數(shù)據(jù)為對(duì)象,否則就會(huì)報(bào)錯(cuò)(其實(shí)這樣寫就是規(guī)避錯(cuò)誤和良好的習(xí)慣)
  }
 },
 methods:{
  EmitchangeOption(index){
  this.$emit('changeOption',index);
   // 通過點(diǎn)擊事件觸發(fā)EmitchangeOption函數(shù),傳入當(dāng)前點(diǎn)擊下拉列表中的索引值index
   // 下拉框通過emit方法觸發(fā)父組件中changeOption函數(shù),動(dòng)態(tài)傳給父組件需要的數(shù)據(jù),這里為索引值
  },
  changeStatus(){
   // 通過changeStatus事件動(dòng)態(tài)改變selectStatus的值,從而控制下拉框的顯示隱藏
  this.selectData.selectStatus = !this.selectData.selectStatus
  }
 }
}
</script>

總結(jié)

  • 從以上的示例可以看出來,父組件傳入數(shù)據(jù),需要在父組件中線綁定一個(gè)屬性,掛載需要傳入的數(shù)據(jù);
  • 子組件接收父組件的數(shù)據(jù)通過 props 方法來接收;
  • 子組件傳遞數(shù)據(jù)需要使用 emit 方法來綁定父組件中事先設(shè)定好的方法,從而動(dòng)態(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;
}
<!--下拉框顯示隱藏動(dòng)畫-->
.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中的父子通訊,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue - vue.config.js中devServer配置方式

    vue - vue.config.js中devServer配置方式

    今天小編就為大家分享一篇vue - vue.config.js中devServer配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • vue-axios使用詳解

    vue-axios使用詳解

    本篇文章主要介紹了vue-axios使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Vue3對(duì)比Vue2的優(yōu)點(diǎn)總結(jié)

    Vue3對(duì)比Vue2的優(yōu)點(diǎn)總結(jié)

    vue3解決了vue2的一些缺陷與弊端,學(xué)習(xí)新的技術(shù)是很有必要的,本文總結(jié)了一些vue3的優(yōu)點(diǎn),希望各位能盡快轉(zhuǎn)入vue3的使用中
    2021-06-06
  • Vue Cli項(xiàng)目重構(gòu)為Vite的方法步驟

    Vue Cli項(xiàng)目重構(gòu)為Vite的方法步驟

    本文主要介紹了Vue Cli項(xiàng)目重構(gòu)為Vite的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 基于Vue中的父子傳值問題解決

    基于Vue中的父子傳值問題解決

    這篇文章主要介紹了基于Vue中的父子傳值問題解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 詳解vue3+element-plus實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由動(dòng)態(tài)按鈕(前后端分離)

    詳解vue3+element-plus實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由動(dòng)態(tài)按鈕(前后端分離)

    本文需要使用axios,路由,pinia,安裝element-plus,并且本文vue3是基于js而非ts的,這些環(huán)境如何搭建不做描述,需要讀者自己完成,感興趣的朋友跟隨小編一起看看吧
    2023-11-11
  • 關(guān)于vue v-for循環(huán)解決img標(biāo)簽的src動(dòng)態(tài)綁定問題

    關(guān)于vue v-for循環(huán)解決img標(biāo)簽的src動(dòng)態(tài)綁定問題

    今天小編就為大家分享一篇關(guān)于vue v-for循環(huán)解決img標(biāo)簽的src動(dòng)態(tài)綁定問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 解決echart在vue中id重復(fù)問題

    解決echart在vue中id重復(fù)問題

    這篇文章主要介紹了解決echart在vue中id重復(fù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue-cli搭建spa項(xiàng)目的項(xiàng)目實(shí)踐

    Vue-cli搭建spa項(xiàng)目的項(xiàng)目實(shí)踐

    本文主要介紹了Vue-cli搭建spa項(xiàng)目的項(xiàng)目實(shí)踐,首先,你需要安裝Vue CLI,然后通過它創(chuàng)建新項(xiàng)目,接著,選擇和配置適當(dāng)?shù)牟寮鸵蕾図?xiàng),以完善你的SPA項(xiàng)目,感興趣的可以了解一下
    2023-09-09
  • 使用Vue實(shí)現(xiàn)Markdown文檔的展示和解析

    使用Vue實(shí)現(xiàn)Markdown文檔的展示和解析

    在Vue項(xiàng)目中,Markdown文檔的使用越來越普遍,因此在Vue中如何進(jìn)行Markdown文檔展示與解析也成為了一個(gè)熱門話題,本文將介紹如何使用Vue實(shí)現(xiàn)Markdown文檔的展示和解析,文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-01-01

最新評(píng)論

方正县| 天水市| 台中县| 石狮市| 永定县| 竹山县| 广灵县| 开远市| 屯昌县| 濉溪县| 萝北县| 宣汉县| 阜城县| 铜陵市| 泸水县| 绥棱县| 贺州市| 嵊泗县| 电白县| 博客| 新丰县| 库车县| 新安县| 临夏市| 阜南县| 隆安县| 宁强县| 精河县| 故城县| 都安| 肥西县| 红桥区| 正定县| 周至县| 五指山市| 孟村| 保德县| 历史| 揭西县| 亳州市| 祥云县|