1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue父子组件的创建和使用</title> </head> <body> <div id="app"> <parent-component :items="books"></parent-component> </div> </body> <template id="parent-component"> <div> <ul> <li v-for="item in items"> {{ item.name }} - {{ item.price }}</li> </ul> <child-component></child-component> </div> </template> <template id="child-component"> <p> 这是一个子组件: {{ child_data }} </p> </template> <script src="js/vue.js" type="text/javascript"></script> <script type="text/javascript"> Vue.component('parent-component', { props: ['items'], template: '#parent-component', components: { 'child-component': { props: ['child-prop'], template: '#child-component', data() { return { child_data: "葵花宝典 - 5000" } } } } });
var vm = new Vue({ el: '#app', data: { books: [{ name: '九阴真经', price: 2000 }, { name: '九阳真经', price: 3000 }, { name: '降龙十八掌', price: 500 }, ] } }) </script> </html>
|