vue 通過 Prop 向子組件傳遞數(shù)據(jù)的實(shí)現(xiàn)方法
這是一個(gè)通過 Prop 向子組件傳遞數(shù)據(jù)的小例子。
代碼:
<!DOCTYPE html><html lang='zh-cmn-Hans'> <head> <meta charset='UTF-8'> <title></title> <style> *{margin: 0;padding: 0;text-decoration: none; } </style> </head> <body> <div id='app'> <!--數(shù)據(jù)的渲染--> <ul><student-component v-for='item in students' :student='item'></student-component> </ul> </div> <script src='http://m.cgvv.com.cn/vue.js'></script> <script> //子組件 //編寫學(xué)生組件 Vue.component(’student-component’,{props:[’student’], // props 可以是數(shù)組或?qū)ο螅糜诮邮諄碜愿附M件的數(shù)據(jù)。template:`<li> <h3>學(xué)生的姓名:{{student.name}}</h3> <h3>學(xué)生的年齡:{{student.age}}</h3> <h3>學(xué)生的興趣:{{student.hobbies}}</h3> <hr/> <br/> </li>` }) //父組件 let app = new Vue({el:’#app’,data:{ //把這些數(shù)據(jù)傳給子組件 然后渲染到頁面上 students:[ { name:’丁七歲’, age:19, hobbies:’吃飯 睡覺 打豆豆’ }, { name:’丁七歲2’, age:19, hobbies:’吃飯 睡覺 打豆豆’ }, { name:’丁七歲3’, age:19, hobbies:’吃飯 睡覺 打豆豆’ } ,{ name:’丁七歲4’, age:19, hobbies:’吃飯 睡覺 打豆豆’ } ]} }) </script> </body></html>
不再關(guān)心dom操作了 專注于數(shù)據(jù)的渲染。比如這個(gè)關(guān)注點(diǎn) 就是如何把 students這個(gè)數(shù)組中的信息渲染到頁面上給用戶看。
到此這篇關(guān)于vue 通過 Prop 向子組件傳遞數(shù)據(jù)的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)vue Prop子組件傳遞數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python爬蟲beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實(shí)現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測試工具Locust安裝及使用7. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式8. 使用Python解析Chrome瀏覽器書簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
