詳解vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot
一、組件間的數(shù)據(jù)傳遞
1.父組件獲取子組件的數(shù)據(jù)
*子組件把自己的數(shù)據(jù),發(fā)送到父級(jí)
*vm.$emit(事件名,數(shù)據(jù));
*v-on: @
示例用法:當(dāng)點(diǎn)擊send按鈕的時(shí)候,“111”變成“我是子組件的數(shù)據(jù)”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父級(jí)獲取子級(jí)的數(shù)據(jù)</title>
<script src="bower_components/vue/dist/vue.js"></script>
<style>
</style>
</head>
<body>
<div>
<aaa>
</aaa>
</div>
<template>
<span>我是父級(jí) -> {{msg}}</span>
//自動(dòng)調(diào)用get方法,@child-msg和下面的this.$emit('child-msg',this.a)相對(duì)應(yīng)
<bbb @child-msg="get"></bbb>
</template>
<template>
<h3>子組件-</h3>
<input type="button" value="send" @click="send">
</template>
<script>
var vm=new Vue({
el:'#box',
data:{
a:'aaa'
},
components:{
'aaa':{
data:function(){
return {
msg:111,
msg2:'我是父組件的數(shù)據(jù)'
}
},
template:'#aaa',
methods:{
//這里的msg實(shí)際上就是子組件傳遞給父組件的數(shù)據(jù)
get:function(msg){
this.msg=msg;
}
},
components:{
'bbb':{
data:function(){
return {
a:'我是子組件的數(shù)據(jù)'
}
},
template:'#bbb',
methods:{
send:function(){
this.$emit('child-msg',this.a);
}
}
}
}
}
}
});
</script>
</body>
</html>
2、子組件獲取父組件的數(shù)據(jù)
在調(diào)用子組件:
<bbb :m="數(shù)據(jù)"></bbb>
子組件之內(nèi):
props:['m','myMsg']
props:{
'm':String,
'myMsg':Number
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自己獲取父級(jí)的數(shù)據(jù)</title>
<script src="bower_components/vue/dist/vue.js"></script>
<style>
</style>
</head>
<body>
<div>
<div>{{a}}</div>
<aaa>
{{msg}}
</aaa>
</div>
<template>
<h1>11111</h1>
<bbb :mmm="msg2" :my-msg="msg"></bbb>
</template>
<script>
var vm=new Vue({
el:'#box',
data:{
a:'a'
},
components:{
'aaa':{
data:function(){
return {
msg:111,
msg2:'我是父組件的數(shù)據(jù)'
}
},
template:'#aa',
components:{
'bbb':{
props:{
'mmm':String,
'myMsg':Number
},
template:'<h3>我是bbb組件->{{mmm}} <br> {{myMsg}}</h3>'
}
}
}
}
});
</script>
</body>
</html>
運(yùn)行結(jié)果:

二、內(nèi)容分發(fā):
Vue.js提供了一種混合父組件內(nèi)容與子組件自己模版的方式:slot,用來占一個(gè)位置
1、基本用法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>slot保留原來的位置</title>
<script src="bower_components/vue/dist/vue.js"></script>
<style>
</style>
</head>
<body>
<div>
<aaa>
<ul>
<li>1111</li>
<li>2222</li>
<li>3333</li>
</ul>
</aaa>
<hr>
<aaa>
</aaa>
</div>
<template>
<h1>xxxx</h1>
<slot>這是默認(rèn)的情況</slot>
<p>welcome vue</p>
</template>
<script>
var vm=new Vue({
el:'#box',
data:{
a:'aaa'
},
components:{
'aaa':{
template:'#aaa'
}
}
});
</script>
</body>
</html>
運(yùn)行結(jié)果:ul標(biāo)簽里面的內(nèi)容沒有被覆蓋,如果不使用slot,ul標(biāo)簽里的內(nèi)容將會(huì)被覆蓋

2、slot的name屬性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>slot中name屬性的使用</title>
<script src="bower_components/vue/dist/vue.js"></script>
<style>
</style>
</head>
<body>
<div>
<aaa>
<ul slot="ul-slot"> //這里slot的名字要與下面slot中name屬性相對(duì)應(yīng)
<li>1111</li>
<li>2222</li>
<li>3333</li>
</ul>
<ol slot="ol-slot"> //用法同上
<li>111</li>
<li>222</li>
<li>333</li>
</ol>
</aaa>
<hr>
<aaa>
</aaa>
</div>
<template>
<h1>xxxx</h1>
<slot name="ol-slot">這是默認(rèn)的情況</slot> //設(shè)置name屬性,給slot命名
<p>welcome vue</p>
<slot name="ul-slot">這是默認(rèn)的情況2</slot>
</template>
<script>
var vm=new Vue({
el:'#box',
data:{
a:'aaa'
},
components:{
'aaa':{
template:'#aaa'
}
}
});
</script>
</body>
</html>
運(yùn)行結(jié)果:

- Vue組件中slot的用法
- 深入理解vue中slot與slot-scope的具體使用
- Vue2.0 slot分發(fā)內(nèi)容與props驗(yàn)證的方法
- vue如何使用 Slot 分發(fā)內(nèi)容實(shí)例詳解
- Vue內(nèi)容分發(fā)slot(全面解析)
- vue Render中slots的使用的實(shí)例代碼
- 詳解Vue學(xué)習(xí)筆記入門篇之組件的內(nèi)容分發(fā)(slot)
- Vue.js中組件中的slot實(shí)例詳解
- 詳解vue slot插槽的使用方法
- Vue.js之slot深度復(fù)制詳解
- Vue中的slot使用插槽分發(fā)內(nèi)容的方法
相關(guān)文章
優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解
這篇文章主要介紹了優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
vue3中關(guān)于路由hash與History的設(shè)置
這篇文章主要介紹了vue3中關(guān)于路由hash與History的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
基于vue2框架的機(jī)器人自動(dòng)回復(fù)mini-project實(shí)例代碼
本篇文章主要介紹了基于vue2框架的機(jī)器人自動(dòng)回復(fù)mini-project實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06
Vue基于el-breadcrumb實(shí)現(xiàn)面包屑功能(操作代碼)
這篇文章主要介紹了Vue基于el-breadcrumb實(shí)現(xiàn)面包屑功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
Vue3?Axios攔截器封裝成request文件的示例詳解
這篇文章主要介紹了Vue3?Axios攔截器封裝成request文件,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
vue項(xiàng)目基于WebRTC實(shí)現(xiàn)一對(duì)一音視頻通話
這篇文章主要介紹了vue項(xiàng)目基于WebRTC實(shí)現(xiàn)一對(duì)一音視頻通話效果,實(shí)現(xiàn)代碼分為前端和后端兩部分代碼,需要的朋友可以參考下2024-05-05
Vue3實(shí)現(xiàn)Element Plus表格的多選功能與條件操作
Element-plus是ElementUI的升級(jí)版,是一套基于vue2與vue3的桌面端組件庫,它提供了豐富的組件幫助開發(fā)人員快速構(gòu)建功能強(qiáng)大、風(fēng)格統(tǒng)一的頁面,本文給大家介紹了Vue3實(shí)現(xiàn)Element Plus表格的多選功能與條件操作,需要的朋友可以參考下2024-08-08
vue實(shí)現(xiàn)ToDoList簡單實(shí)例
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)ToDoList簡單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

