props父把信息传递给子组件
1父组件
<template> 2 <div class="hello"> 3 <div id="app-3" v-on:click="show"> 4 点击显示隐藏 5 <p v-if="seen">现在可以看到我了</p> 6 </div> 7 <div class="app-4"> 8 <ol> 9 <tab v-for="item in groceryList" v-bind:todo="item">
<!--:todo--> 10 </tab> 11 </ol> 12 </div> 13 <div class="app-5"> 14 <p>{{message}}</p> 15 <input v-model="message"> 16 <button v-on:click="reverse">逆转信息</button> 17 </div> 18 </div> 19 20 </template> 21 22 <script> 23 import Tab from '@/components/tab/tab' 24 export default { 25 name: 'hello', 26 components:{Tab}, 27 data () { 28 return { 29 message:'Hello Vue!', 30 seen:true, 31 todos:[ 32 {text:'学习javascript'}, 33 {text:'学习Vue'} 34 ], 35 groceryList: [ 36 { id: 0, text: '蔬菜' }, 37 { id: 1, text: '奶酪' }, 38 { id: 2, text: '随便其他什么人吃的东西' } 39 ] 40 } 41 }, 42 methods:{ 43 show(){ 44 this.seen=!this.seen; 45 }, 46 reverse(){ 47 this.message=this.message.split('').reverse().join(''); 48 } 49 } 50 } 51 </script> 52 53 <style scoped> 54 h1, h2 { 55 font-weight: normal; 56 } 57 58 ul { 59 list-style-type: none; 60 padding: 0; 61 } 62 63 li { 64 display: inline-block; 65 margin: 0 10px; 66 } 67 68 a { 69 color: #42b983; 70 } 71 </style>
/****
******
*******
**********
*********/
子组件
1 <template> 2 <li>{{todo.id}}{{todo.text}}</li> 3 </template> 4 <script> 5 export default{ 6 props:['todo'],/*通过这个把数据传过来*/ 7 data(){ 8 return{ 9 10 } 11 }, 12 methods:{ 13 14 } 15 } 16 </script>
子组件传值给父元素
1 只展示重要代码 2 子组件 3 this.$emit('changeLocale',locale); 4 父组件 5 <v-header @changeLocale="paLocale"></v-header> 6 方法 7 paLocale(evtValue){ 8 //evtValue表示子组件传递过来的locale值 9 },
简单的 弹窗组件编写
//父组件 <template> <div class="parent"> <button @click="showModal">按钮</button> <child v-if="showModel" header-title="温馨提示" @close="showModel = false" @closeModel="closeModel"> <div slot="body"> <p>你是傻逼吗?</p> <p>你是!</p> </div> </child> </div> </template> <script> import child from '@/components/sub/child' export default{ components:{child}, data(){ return{ showModel:false } }, methods:{ showModal(){ this.showModel=true }, closeModel(){ this.showModel=false } } } </script> <style> </style> //子组件 <template> <transition name="model"> <div class="model" > <div class="model-mask" @click="close"></div> <div class="modal-wrapper"> <div class="modal-header"> <slot name="header"> {{headerTitle}} </slot> </div> <div class="modal-body"> <slot name="body"> default body </slot> </div> <div class="modal-footer"> <slot name="footer"> <button class="modal-default-button" @click="close"> OK </button> </slot> </div> </div> </div> </transition> </template> <script> export default{ props:['headerTitle'], methods:{ close(){ this.$emit('closeModel'); } } } </script> <style scoped> .model{ position: fixed; z-index: 96; top: 0; left: 0; 100%; height: 100%; } .model-mask{ position: fixed; z-index: 97; top: 0; left: 0; 100%; height: 100%; background-color: rgba(0, 0, 0, .5); transition: opacity .3s ease; } .modal-wrapper{ position: relative; 80%; height: auto; margin: 0 auto; margin-top: 100px; padding: 20px 30px; background-color: #fff; border-radius: 2px; box-shadow: 0 2px 8px rgba(0, 0, 0, .33); transition: all .3s ease; z-index:98; } .model-enter-active, .model-leave-active{ transition: opacity .5s; opacity:1; } .model-enter, .model-leave-to { opacity: 0 } </style>