vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能示例
本文實(shí)例講述了vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能。分享給大家供大家參考,具體如下:
一、vue默認(rèn)情況下,子組件也沒法訪問父組件數(shù)據(jù)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <style> </style> </head> <body> <div id="box"> <aaa> </aaa> </div> <script> var vm=new Vue({ el:'#box', data:{ a:'aaa' }, components:{ 'aaa':{ data(){ return { msg:'我是父組件的數(shù)據(jù)' } }, template:'<h2>我是aaa組件</h2><bbb></bbb>', components:{ 'bbb':{ template:'<h3>我是bbb組件->{{msg}}</h3>'//這里是子組件,是訪問不到父組件的數(shù)據(jù)msg } } } } }); </script> </body> </html>
二、數(shù)據(jù)傳遞
組件數(shù)據(jù)傳遞: √
1. 子組件獲取父組件data
在調(diào)用子組件:
<bbb :m="數(shù)據(jù)"></bbb>
子組件之內(nèi):
props:['m','myMsg'] props:{ 'm':String, 'myMsg':Number }
2. 父級獲取子級數(shù)據(jù)
*子組件把自己的數(shù)據(jù),發(fā)送到父級
vm.$emit(事件名,數(shù)據(jù));
v-on: @
1、子組件獲取父組件data
方法一:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <style> </style> </head> <body> <div id="box"> <aaa></aaa> </div> <template id="aaa"> <h1>11111</h1> <bbb :mmm="msg2" :my-msg="msg"></bbb> </template> <script> var vm=new Vue({ el:'#box', data:{ a:'aaa' }, components:{ 'aaa':{ data(){ return { msg:111, msg2:'我是父組件的數(shù)據(jù)' } }, template:'#aaa', components:{ 'bbb':{ props:['mmm','myMsg'],//my-msg在這里要變成駝峰命名法 template:'<h3>我是bbb組件->{{mmm}} <br> {{myMsg}}</h3>' } } } } }); </script> </body> </html>
方法二:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <style> </style> </head> <body> <div id="box"> <aaa></aaa> </div> <template id="aaa"> <h1>11111</h1> <bbb :mmm="msg2" :my-msg="msg"></bbb> </template> <script> var vm=new Vue({ el:'#box', data:{ a:'aaa' }, components:{ 'aaa':{ data(){ return { msg:111, msg2:'我是父組件的數(shù)據(jù)' } }, template:'#aaa', components:{ 'bbb':{ props:{ 'm':String,//注明數(shù)據(jù)類型 'myMsg':Number }, template:'<h3>我是bbb組件->{{mmm}} <br> {{myMsg}}</h3>' } } } } }); </script> </body> </html>
2、 父級獲取子級數(shù)據(jù)
方法一:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <style> </style> </head> <body> <div id="box"> <aaa></aaa> </div> <template id="aaa"> <span>我是父級 -> {{msg}}</span> <bbb @child-msg="get"></bbb> </template> <template id="bbb"> <h3>子組件-</h3> <input type="button" value="send" @click="send"> </template> <script> var vm=new Vue({ el:'#box', data:{ a:'aaa' }, components:{ 'aaa':{ data(){ return { msg:'我是父組件的數(shù)據(jù)' } }, template:'#aaa', methods:{ get(msg){ // alert(msg); this.msg=msg; } }, components:{ 'bbb':{ data(){ return { a:'我是子組件的數(shù)據(jù)' } }, template:'#bbb', methods:{ send(){ this.$emit('child-msg',this.a); } } } } } } }); </script> </body> </html>
注意:
- vm.dispatch(事件名,數(shù)據(jù))子級向父級發(fā)送數(shù)據(jù)vm.dispatch(事件名,數(shù)據(jù))子級向父級發(fā)送數(shù)據(jù)vm.broadcast(事件名,數(shù)據(jù)) 父級向子級廣播數(shù)據(jù)
- 配合: event:{}
- 在vue2.0里面已經(jīng),報廢了
slot:位置、槽口
作用: 占個位置,不覆蓋原先的內(nèi)容
類似ng里面 transclude (指令)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <style> </style> </head> <body> <div id="box"> <aaa> <ul slot="ul-slot"> <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 id="aaa"> <h1>xxxx</h1> <slot name="ol-slot">這是默認(rèn)的情況</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>
效果圖:
vue-> SPA應(yīng)用,單頁面應(yīng)用 vue-router路由
vue-resouce 交互
vue-router 路由
路由:根據(jù)不同url地址,出現(xiàn)不同效果
該課程配套用 0.7.13版本 vue-router
主頁 home
新聞頁 news
html:
<a v-link="{path:'/home'}">主頁</a> 跳轉(zhuǎn)鏈接 展示內(nèi)容: <router-view></router-view>
js:
//1. 準(zhǔn)備一個根組件 var App=Vue.extend(); //2. Home News組件都準(zhǔn)備 var Home=Vue.extend({ template:'<h3>我是主頁</h3>' }); var News=Vue.extend({ template:'<h3>我是新聞</h3>' }); //3. 準(zhǔn)備路由 var router=new VueRouter(); //4. 關(guān)聯(lián) router.map({ 'home':{ component:Home }, 'news':{ component:News } }); //5. 啟動路由 router.start(App,'#box');
跳轉(zhuǎn):
router.redirect({ '/':'/home' });
下載vue-router:
vue-router路由:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <script src="bower_components/vue-router/dist/vue-router.js"></script> <style> </style> </head> <body> <div id="box"> <ul> <li> <a v-link="{path:'/home'}">主頁</a> </li> <li> <a v-link="{path:'/news'}">新聞</a> </li> </ul> <div> <router-view></router-view> </div> </div> <script> //1. 準(zhǔn)備一個根組件 var App=Vue.extend(); //2. Home News組件都準(zhǔn)備 var Home=Vue.extend({ template:'<h3>我是主頁</h3>' }); var News=Vue.extend({ template:'<h3>我是新聞</h3>' }); //3. 準(zhǔn)備路由 var router=new VueRouter(); //4. 關(guān)聯(lián) router.map({ 'home':{ component:Home }, 'news':{ component:News } }); //5. 啟動路由 router.start(App,'#box'); </script> </body> </html>
跳轉(zhuǎn):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script> <script src="bower_components/vue-router/dist/vue-router.js"></script> <style> </style> </head> <body> <div id="box"> <ul> <li> <a v-link="{path:'/home'}">主頁</a> </li> <li> <a v-link="{path:'/news'}">新聞</a> </li> </ul> <div> <router-view></router-view> </div> </div> <script> //1. 準(zhǔn)備一個根組件 var App=Vue.extend(); //2. Home News組件都準(zhǔn)備 var Home=Vue.extend({ template:'<h3>我是主頁</h3>' }); var News=Vue.extend({ template:'<h3>我是新聞</h3>' }); //3. 準(zhǔn)備路由 var router=new VueRouter(); //4. 關(guān)聯(lián) router.map({ 'home':{ component:Home }, 'news':{ component:News } }); //5. 啟動路由 router.start(App,'#box'); //6. 跳轉(zhuǎn) router.redirect({ '/':'home' //訪問根目錄時,跳轉(zhuǎn)到/home }); </script> </body> </html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運(yùn)行效果。
希望本文所述對大家vue.js程序設(shè)計有所幫助。
- vue父子組件的數(shù)據(jù)傳遞示例
- Vue 父子組件的數(shù)據(jù)傳遞、修改和更新方法
- Vue表單類的父子組件數(shù)據(jù)傳遞示例
- Vue 父子組件數(shù)據(jù)傳遞的四種方式( inheritAttrs + $attrs + $listeners)
- vuejs父子組件之間數(shù)據(jù)交互詳解
- vue2.0父子組件間傳遞數(shù)據(jù)的方法
- vue組件之間數(shù)據(jù)傳遞的方法實(shí)例分析
- Vue組件之單向數(shù)據(jù)流的解決方法
- vue兄弟組件傳遞數(shù)據(jù)的實(shí)例
- Vue.js路由組件vue-router使用方法詳解
- vue-router:嵌套路由的使用方法
相關(guān)文章
Vue中使用vue2-perfect-scrollbar制作滾動條
這篇文章主要介紹了Vue中使用vue2-perfect-scrollbar滾動條,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06解決vue做詳情頁跳轉(zhuǎn)的時候使用created方法 數(shù)據(jù)不會更新問題
這篇文章主要介紹了解決vue做詳情頁跳轉(zhuǎn)的時候使用created方法 數(shù)據(jù)不會更新問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue實(shí)現(xiàn)導(dǎo)入json解析成動態(tài)el-table樹表格
本文主要介紹了vue實(shí)現(xiàn)導(dǎo)入json解析成動態(tài)el-table樹表格,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02vue轉(zhuǎn)react useEffect的全過程
這篇文章主要介紹了vue轉(zhuǎn)react useEffect的全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09