Javascript的爺孫通信和組件自調(diào)用詳解
更新時間:2022年03月29日 14:08:21 作者:時間不夠以後
這篇文章主要為大家詳細(xì)介紹了Javascript的爺孫通信和組件自調(diào)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1.組件自己調(diào)用自己
父組件
<template>
<div>
<detail-list :list="categoryList"></detail-list>
</div>
</template>
<script>
import DetailList from './detailList.vue'
export default {
components: { DetailList },
data () {
return {
categoryList: [
{
title: '1',
children: [
{
title: '1-1'
},
{
title: '1-2'
},
]
},
{
title: '2',
children: [
{
title: '2-1'
},
{
title: '2-2'
},
]
}
]
}
}
}
</script>
子組件
<template>
<template>
<div>
<!--遞歸組件的應(yīng)用===》可以通過組件命名來自己使用自己的組件-->
<div class="item" v-for="(item, index) in list" :key="index">
<div class="item-title border-bottom">
<span class="item-title-icon"></span>
{{ item.title }}
</div>
<div v-if="item.children" class="item-children">
<detail-list :list="item.children"></detail-list>
<!-- //自己使用自己的組件detailList -->
</div>
</div>
</div>
</template>
<script>
export default {
name: 'DetailList', //組件命名
props: {
list: Array,
},
data() {
return {}
},
}
</script>
爺孫通信

grand.vue
<template>
<div>
<detail-list :list="categoryList"></detail-list>
</div>
</template>
<script>
import DetailList from './detailList.vue'
export default {
components: { DetailList },
data () {
return {
categoryList: [
{
title: '1',
children: [
{
title: '1-1'
},
{
title: '1-2'
},
]
},
{
title: '2',
children: [
{
title: '2-1'
},
{
title: '2-2'
},
]
}
]
}
}
}
</script>
father.vue
<template>
<template>
<div>
<!--遞歸組件的應(yīng)用===》可以通過組件命名來自己使用自己的組件-->
<div class="item" v-for="(item, index) in list" :key="index">
<div class="item-title border-bottom">
<span class="item-title-icon"></span>
{{ item.title }}
</div>
<div v-if="item.children" class="item-children">
<detail-list :list="item.children"></detail-list>
<!-- //自己使用自己的組件detailList -->
</div>
</div>
</div>
</template>
<script>
export default {
name: 'DetailList', //組件命名
props: {
list: Array,
},
data() {
return {}
},
}
</script>
chidren.vue
<template>
<div>
??爺爺
<br>
<div>GrandSon的回復(fù):{{reply}}</div>
<father :msg1="msg1" :msg2="msg2" @getReply="getReply"></father>
</div>
</template>
<script>
import Father from './father.vue'
export default {
components: { Father },
data () {
return {
msg1: '1??我是GrandFather,把第二條傳給GrandSon',
msg2: '2??GrandSon你好,我是GrandFather',
reply: '' // 接收來自GrandSon的消息
}
},
methods: {
/* 將獲得的數(shù)據(jù)綁定到data中,便于視圖層渲染 */
getReply (param) {
this.reply = param
}
}
}
</script>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
坐標(biāo)軸刻度取值算法之源于echarts的y軸刻度計算需求
坐標(biāo)軸刻度作為直角坐標(biāo)系中重要的組成部分,我們需要學(xué)會合理的設(shè)置坐標(biāo)軸的刻度,下面這篇文章主要給大家介紹了關(guān)于坐標(biāo)軸刻度取值算法之源于echarts的y軸刻度計算需求的相關(guān)資料,需要的朋友可以參考下2022-06-06
js實(shí)現(xiàn)按Ctrl+Enter發(fā)送效果
按Ctrl+Enter發(fā)送,思路是監(jiān)聽textarea的onkeydown事件,當(dāng)ctrl鍵被按下,并且,keycode為13(回車),時,調(diào)用發(fā)送表單的函數(shù)2014-09-09
關(guān)于JS Lodop打印插件打印Bootstrap樣式錯亂問題的解決方案
關(guān)于Lodop打印是個很牛的打印插件,但是打印Bootstrap的樣式的時候就容易錯亂,下面通過本文給大家分享下這方面的問題2016-12-12
javascript中數(shù)組的定義及使用實(shí)例
這篇文章主要介紹了javascript中數(shù)組的定義及使用方法,實(shí)例分析了數(shù)組的定義及使用技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-01-01
使用javascript做的一個隨機(jī)點(diǎn)名程序
這篇文章主要介紹了使用javascript做的一個隨機(jī)點(diǎn)名程序,經(jīng)測試,效果相當(dāng)不錯,需要的朋友可以參考下2014-02-02
JavaScript實(shí)現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法,涉及JavaScript編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2016-01-01

