詳解vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot
一、組件間的數(shù)據(jù)傳遞
1.父組件獲取子組件的數(shù)據(jù)
*子組件把自己的數(shù)據(jù),發(fā)送到父級
*vm.$emit(事件名,數(shù)據(jù));
*v-on: @
示例用法:當(dāng)點擊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ù)據(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>
運行結(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>
運行結(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>
運行結(jié)果:
相關(guān)文章
vue?使用addRoutes動態(tài)添加路由及刷新頁面跳轉(zhuǎn)404路由的問題解決方案
我自己使用addRoutes動態(tài)添加的路由頁面,使用router-link標(biāo)簽可以跳轉(zhuǎn),但是一刷新就會自動跳轉(zhuǎn)到我定義的通配符?*?指向的404路由頁面,這說明沒有找到指定路由才跳到404路由的,這樣的情況如何處理呢,下面小編給大家分享解決方案,一起看看吧2023-10-10vue?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/cli4.x版本的vue.config.js常用配置方式
這篇文章主要介紹了@vue/cli4.x版本的vue.config.js常用配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05