¸ø×Ó×é¼þÉÏ×¢²áÒýÓÃÐÅÏ¢ ref="page1" £¬Äõ½¸ÄÒýÓÃÐÅÏ¢ this.$refs.page1 £¬ÆäʵҲ¾ÍÊÇ»ñÈ¡×Ó×é¼þµÄÒâ˼£¬ ½Ó×ŵ÷ÓÃ×Ó×é¼þµÄ·½·¨ this.$refs.page1.handleParentClick() ; handleParentClick ÊÇ·½·¨Ãû£¬
¿ÉÒÔ×Ô¶¨£¬Ö»Òª×Ó×é¼þÒ²ÓжÔÓ¦µÄ·½·¨Ãû¼´¿É¡£
<template> <div id="appVue"> <div class='ddiv' @click="divClick" ></div> <page1 ref="page1" :infodata="infodata"></page1> </div> </template> <script type="text/ecmascript-6"> import page1 from './components/page1/page1'; export default { name: 'appVue', data(){ return { infodata:{ ‘name’:’ÔøÌïÉú’£¬ ‘age’:'24' } } }, methods:{ divClick:function(){ //µ÷ÓÃ×Ó×é¼þ page1 µÄ handleParentClick ·½·¨ this.$refs.page1.handleParentClick(); } }, components: { page1 } } </script> <style lang="scss"> </style>×Ó×é¼þ£º
×Ó×é¼þÔÚ methods ¶ÔÏóÀïÉùÃ÷ ±»¸¸×é¼þµ÷Óõķ½·¨ handleParentClick
<template> <div id="page1"> <p>{{infodata.name}}</p> <p>{{infodata.age}}</p> </div> </template> <script type="text/ecmascript-6"> export default { props: { infodata: { type: Object, default: function () { return{ ‘name’:’xxx’£¬ ‘age’:'111' } } } }£¬ watch£º{ ’infodata’:function(){ console.log('infodata Êý¾Ý±ä»¯»á´¥·¢ÎÒÕâ¸ö·½·¨µÄÖ´ÐÐ' ); } }£¬ methods:{ handleParentClick: function () { console.log('¸¸×é¼þ»½ÆðÁ˸÷½·¨µÄÖ´ÐÐ' ); } } } </script> <style lang="scss"> </style> 5¡¢×Ó×é¼þ¸ø¸¸×é¼þ»Ø´«Êý¾Ý ÉÏÃæµÚ4µã½²Á˸¸×é¼þ¿ÉÒÔµ÷ÓÃ×Ó×é¼þµÄ·½·¨£¬µ«ÊÇ ×Ó×é¼þÊDz»Äܵ÷Óø¸×é¼þµÄ·½·¨µÄ£¬ÎªÁ˱£»¤¸¸×é¼þ Êý¾Ý²»±»ÎÛȾ»òÆÆ»µ¡£µ« vue Ò²ÓÃÁËÒ»Ì×·½·¨ À´½«×Ó×é¼þµÄÊý¾Ý»Ø´«¸ø¸¸×é¼þ¡£ ×é¼þ³ýÁË¿ÉÒÔ¸ø div ¼ÓÉÏ `v-on:click = 'doSomething'` À´¶Ô div¼àÌýµã»÷ʼþÍ⣬¸¸×é¼þ»¹¿ÉÒÔ Óà `v-on:name='listenChild' `À´¼àÌý×Ó×é¼þ´«µÝ¸ø¸¸×é¼þµÄÊý¾Ý¡£¸¸×é¼þ£º
¸¸×é¼þ¿ÉÒÔÔÚʹÓÃ×Ó×é¼þµÄµØ·½Ö±½ÓÓà v-on À´¼àÌý×Ó×é¼þ´¥·¢µÄʼþ¡£
<template> <div id="appVue"> <div class='ddiv' @click="divClick" ></div> <page1 v-on:page-to-appvue='listenChild' ref="page1" :infodata="infodata"></page1> </div> </template> <script type="text/ecmascript-6"> import page1 from './components/page1/page1'; export default { name: 'appVue', data(){ return { infodata:{ ‘name’:’ÔøÌïÉú’£¬ ‘age’:'24' } } }, methods:{ listenChild: function (msg) { console.log('¼àÌý×Ó×é¼þ´«µÝ¹ýÀ´µÄÊý¾Ý'+msg+‘²¢´¥·¢¸Ä·½·¨µÄÖ´ÐÐ’ ); }£¬ divClick:function(){ //µ÷ÓÃ×Ó×é¼þ page1 µÄ handleParentClick ·½·¨ this.$refs.page1.handleParentClick(); } }, components: { page1 } } </script> <style lang="scss"> </style>×Ó×é¼þ£º
ʹÓà $emit(eventName) ´¥·¢Ê¼þ
<template> <div id="page1"> <div @click="divClick" ></div> <p>{{infodata.name}}</p> <p>{{infodata.age}}</p> </div> </template> <script type="text/ecmascript-6"> export default { props: { infodata: { type: Object, default: function () { return{ ‘name’:’xxx’, ‘age’:'111' } } } }£¬ watch£º{ ’infodata’:function(){ console.log('infodata Êý¾Ý±ä»¯»á´¥·¢ÎÒÕâ¸ö·½·¨µÄÖ´ÐÐ' ); } }£¬ methods:{ divClick:function(){ // ¸ø¸¸×é¼þ·¢ËÍÏûÏ¢ this.$emit('page-to-appvue',{‘name’:’xxx’,‘age’:'111'}); }, handleParentClick: function () { console.log('¸¸×é¼þ»½ÆðÁ˸÷½·¨µÄÖ´ÐÐ' ); } } } </script> <style lang="scss"> </style> Èý¡¢nodejs Ä£ÄâÏßÉÏÊý¾Ýµ÷ÊÔ ×öÇ°¶Ë¿ª·¢Ò»°ã²»»áÖ»×ö¾²Ì¬Ò³Ã棬ÌرðÊÇʹÓà vue ¿ò¼ÜµÄʱºò£¬Ò»°ãÊǾßÓи´ÔÓµãµÄÊý¾Ý½»»¥Äã²ÅÑ¡ÔñËü¡£ ÄǾÍÃâ²»ÁËÐèºǫ́Êý¾ÝµÄµ÷ÊÔ£¬ÔÛÃÇ¿ÉÒÔÏÈдºÃ½Ó¿Ú£¬ÔÙÄ£ÄâһϺǫ́Êý¾Ý£¬µ½Ê±ºòÇ°ºó¶ËÊý¾ÝÁªµ÷ ¾Í»áÇáËɶàÁË 1¡¢¿ª·¢Öеĵ÷ÊÔÔÚÏîÄ¿µÄ¸úĿ¼ÏÂÄ£ÄâÒ»·ÝjsonÊý¾Ý£º
¡¡