vue 通過 Prop 向子組件傳遞數(shù)據(jù)的實現(xiàn)方法
這是一個通過 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.lshqa.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ù)的渲染。比如這個關(guān)注點 就是如何把 students這個數(shù)組中的信息渲染到頁面上給用戶看。
到此這篇關(guān)于vue 通過 Prop 向子組件傳遞數(shù)據(jù)的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)vue Prop子組件傳遞數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JAMon(Java Application Monitor)備忘記2. 如何清空python的變量3. Python 如何展開嵌套的序列4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. Spring security 自定義過濾器實現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實例代碼)6. Java類加載機制實現(xiàn)步驟解析7. Python TestSuite生成測試報告過程解析8. Python os庫常用操作代碼匯總9. Python OpenCV去除字母后面的雜線操作10. 增大python字體的方法步驟
