詳解vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot
一、組件間的數(shù)據(jù)傳遞
1.父組件獲取子組件的數(shù)據(jù)
*子組件把自己的數(shù)據(jù),發(fā)送到父級
*vm.$emit(事件名,數(shù)據(jù));
*v-on: @
示例用法:當(dāng)點(diǎn)擊send按鈕的時候,“111”變成“我是子組件的數(shù)據(jù)”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父級獲取子級的數(shù)據(jù)</title>
<script src="bower_components/vue/dist/vue.js"></script>
<style>
</style>
</head>
<body>
<div>
<aaa>
</aaa>
</div>
<template>
<span>我是父級 -> {{msg}}</span>
//自動調(diào)用get方法,@child-msg和下面的this.$emit('child-msg',this.a)相對應(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>自己獲取父級的數(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,用來占一個位置
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)容將會被覆蓋

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屬性相對應(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)文章
vue?使用addRoutes動態(tài)添加路由及刷新頁面跳轉(zhuǎn)404路由的問題解決方案
我自己使用addRoutes動態(tài)添加的路由頁面,使用router-link標(biāo)簽可以跳轉(zhuǎn),但是一刷新就會自動跳轉(zhuǎn)到我定義的通配符?*?指向的404路由頁面,這說明沒有找到指定路由才跳到404路由的,這樣的情況如何處理呢,下面小編給大家分享解決方案,一起看看吧2023-10-10
vue?scss后綴文件background-image路徑錯誤的解決
這篇文章主要介紹了vue?scss后綴文件background-image路徑錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
使用this.$nextTick()獲取不到數(shù)據(jù)更新后的this.$refs.xxx.及場景分析
今天遇到了這樣一個場景,在數(shù)據(jù)更新之后,使用this.$nextTick(()=>{console.log(this.$refs.xxx)}) 獲取不到改dom,但是用setTimeout能夠獲取到,在此記錄一下,感興趣的朋友跟隨小編一起看看吧2023-02-02
解決vue中菜單再次點(diǎn)擊內(nèi)容不刷新問題
當(dāng)elementUI中菜單打開后,再次點(diǎn)擊不會刷新的問題,導(dǎo)致菜單再次點(diǎn)擊不刷新的根本原因是頁面打開后,再次打開相同的頁面是不會刷新的,這應(yīng)該是框架的機(jī)制就是如此,小編整理了兩個比較不錯的解決方法,需要的朋友可以參考下2023-08-08
@vue/cli4.x版本的vue.config.js常用配置方式
這篇文章主要介紹了@vue/cli4.x版本的vue.config.js常用配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05

