vue中父子組件的參數(shù)傳遞和應用示例
1.在父組件中調(diào)用子組件,父親傳值給子組件
子組件如下,把要傳給給父親的值放在props中
template>
<!--底部導航-->
<div class="index-bbar">
<ul class="flex" >
<li v-for="(item,index) in liAry" :class="index==licurrent?'active':''">
<router-link :to="item.linkURl">
<span class="flex alignc flexdc">
<img :src="index==licurrent?require('../../' + item.urlActive):require('../../' + item.url)" class="img1" ><span>{{item.title}}</span>
</span>
</router-link>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'Bottom',
data () {
return {
}
},
props:['liAry','licurrent'],
methods: {
}
}
</script>
<style scoped>
@import "../../assets/public/css/top.css";
@import "../../assets/public/css/bottom.css";
</style>
父組件的調(diào)用的三部曲
首先引入子組件
import Bottom from '@/components/public/Bottom';
注入組件在components中注入
components: {Bottom}
在父親中應用
<template> <Bottom v-bind:liAry='lidata' v-bind:licurrent='guidecurrent'></Bottom> </template>
到這里就結束了,是不是賊快
2.子組件傳值給父組件
父組件在組件上定義了一個自定義事件childFn,事件名為parentFn用于接受子組件傳過來的message值。
<!-- 父組件 -->
<template>
<div class="test">
<test-com @childFn="parentFn"></test-com>
<br/>
子組件傳來的值 : {{message}}
</div>
</template>
<script>
export default {
// ...
data: {
message: ''
},
methods: {
parentFn(payload) {
this.message = payload;
}
}
}
</script>
子組件是一個buttton按鈕,并為其添加了一個click事件,當點擊的時候使用$emit()觸發(fā)事件,把message傳給父組件
<!-- 子組件 -->
<template>
<div class="testCom">
<input type="text" v-model="message" />
<button @click="click">Send</button>
</div>
</template>
<script>
export default {
// ...
data() {
return {
// 默認
message: '我是來自子組件的消息'
}
},
methods: {
click() {
this.$emit('childFn', this.message);
}
}
}
</script>
在子組件向父親傳值的時候,不可用router-link,不然接受不到父親定義的函數(shù)
以上就是vue中父子組件的參數(shù)傳遞和應用示例的詳細內(nèi)容,更多關于vue中父子組件的參數(shù)傳遞的資料請關注腳本之家其它相關文章!
相關文章
Vue中$emit調(diào)用父組件異步方法模擬.then實現(xiàn)方式
這篇文章主要介紹了Vue中$emit調(diào)用父組件異步方法模擬.then實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
Vue Computed中get和set的用法及Computed與watch的區(qū)別
這篇文章主要介紹了Vue Computed中get和set的用法及Computed與watch的區(qū)別,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
vue-infinite-loading2.0 中文文檔詳解
本篇文章主要介紹了vue-infinite-loading2.0 中文文檔詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
vue+node+socket io實現(xiàn)多人互動并發(fā)布上線全流程
這篇文章主要介紹了vue+node+socket io實現(xiàn)多人互動并發(fā)布上線全流程,本文給大家提到了socket.io相關用法概覽及開發(fā)流程,需要的朋友可以參考下2021-09-09

