国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

詳解vue中在父組件點擊按鈕觸發子組件的事件

瀏覽:4日期:2022-10-30 10:50:11

我把這個實例分為幾個步驟解讀:

1、父組件的button元素綁定click事件,該事件指向notify方法2、給子組件注冊一個ref=“child”3、父組件的notify的方法在處理時,使用了$refs.child把事件傳遞給子組件的parentMsg方法,同時攜帶著父組件中的參數msg 4、子組件接收到父組件的事件后,調用了parentMsg方法,把接收到的msg放到message數組中

父組件

<template> <div id='app'> <!--父組件--> <input v-model='msg' /> <button v-on:click='notify'>廣播事件</button> <!--子組件--> <popup ref='child'></popup> </div></template> <script>import popup from '@/components/popup';export default { name: 'app', data: function () { return { msg: '', }; }, components: { popup, }, methods: { notify: function () { if (this.msg.trim()) { this.$refs.child.parentMsg(this.msg); } }, },};</script> <style>#app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px;}</style>

子組件

<template> <div> <ul> <li v-for='item in messages'>父組件輸入了:{{ item }}</li> </ul> </div></template> <style>body { background-color: #ffffff;}</style> <script>export default { name: 'popup', data: function () { return { messages: [], }; }, methods: { parentMsg: function (msg) { this.messages.push(msg); }, },};</script>

到此這篇關于詳解vue中在父組件點擊按鈕觸發子組件的事件的文章就介紹到這了,更多相關vue 父組件觸發子組件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 贺兰县| 伊宁市| 苍南县| 五河县| 思茅市| 获嘉县| 城口县| 乌兰浩特市| 松原市| 鹤山市| 深水埗区| 康定县| 平山县| 北川| 津南区| 息烽县| 县级市| 九寨沟县| 来宾市| 锡林郭勒盟| 谢通门县| 鹤山市| 丘北县| 普洱| 永顺县| 青冈县| 团风县| 梓潼县| 宁德市| 黄石市| 旺苍县| 定陶县| 镇远县| 永昌县| 宣武区| 阳朔县| 四会市| 石阡县| 瑞昌市| 华蓥市| 藁城市|