<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html</title> <link rel="stylesheet" href="http://unpkg.com/iview/dist/styles/iview.css"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script src="http://unpkg.com/iview/dist/iview.min.js"></script> </head> <body> <div id="page"> <com-header></com-header> <h3>----------------</h3> <com-footer></com-footer> <h2>===============对话列表====================</h2> <ul v-if="dhList && dhList.length"> <template v-for="(item,index) in dhList"> <li><a @click="remove(index)">X</a> {{item}}</li> </template> </ul> </div> <script type="text/x-template" id="comHeaderTemplate"> <div> <button @click="sendMess()"> {{msg}} </button> <input type="text" v-model="userMess" @keyup.13="sendMess()"> </div> </script> <script type="text/x-template" id="comFoooterTemplate"> <div> <button @click="sendMess()" > {{msg}} </button> <input type="text" v-model="userMess" @keyup.13="sendMess()"> </div> </script> <script> var pu = new Vue(); var dhList = []; var comHeader = { template:'#comHeaderTemplate', data:function(){ return { msg:'header', userMess:'' } }, methods:{ sendMess:function(){ pu.$emit('mess',this.userMess); this.userMess = ''; } }, mounted:function(){ pu.$on('mess2',function(m2){ console.log("footer:"+m2) dhList.push("footer:"+m2); }) } } var comFooter = { template:'#comFoooterTemplate', data:function(){ return { msg:'footer', userMess:'' } }, methods:{ sendMess:function(){ pu.$emit('mess2',this.userMess); this.userMess = ''; } }, mounted:function(){ pu.$on('mess',function(m){ console.log("header:"+m); dhList.push("header:"+m); }) } } new Vue({ el:'#page', data:{ dhList:dhList }, methods:{ remove:function(index){ this.dhList.splice(index,1); } }, components:{ comHeader:comHeader, comFooter:comFooter } }) </script> </body> </html>