javascript - 子組件觸發(fā)父組件的自定義事件 父組件無任何反應(yīng)
問題描述
以下為子組件 @change=’showChange’為子組件事件以下模板注冊(cè)為 order-type組件
<template><select name='dType' v-el:select @change=’showChange’> <option value='' v-if='type==’selectAll’'>全部</option> <option v-for='branch in branchList' :value='branch.id' track-by='$index'>{{branch.name}} </option> </select></template>
以下為子組件方法:
showChange(event) { for (let branch of this.branchList) {if (branch[’id’] === event.target.value) { this.$emit(’showChange’,branch[’prefix’]);} }
以下是父組件
<order-type @showChange=’alert(2)’></order-type>
但alert(2) 并未執(zhí)行
問題解答
回答1:你直接這么寫有問題的吧應(yīng)該是
<order-type @showChange=’alertFun’></order-type> 父組件有一個(gè)方法methods: { alertFun () {alert(2) }}
這里應(yīng)該傳遞的是父組件方法的一個(gè)函數(shù)名,而不是直接寫alert(2)
回答2:應(yīng)該是這塊出問題了<option v-for='branch in branchList' :value='branch.id' track-by='$index'>for in對(duì)象循環(huán)取得的是索引,不是值,所以取不到branch.id,可以改成for of
回答3:以下為子組件 @change=’showChange’為子組件事件以下模板注冊(cè)為 order-type組件
<template><select name='dType' v-el:select @change:parentChage=’showChange’>
<option value='' v-if='type==’selectAll’'>全部</option> <option v-for='branch in branchList' :value='branch.id' track-by='$index'> {{branch.name}} </option>
</select></template>
以下為子組件方法:
showChange(event) {for (let branch of this.branchList) { if (branch[’id’] === event.target.value) { /注意此行的修改/ this.$emit(’parentChage’,branch[’prefix’]); }}以下是父組件<order-type @showChange=’alert(2)’></order-type> 但alert(2) 并未執(zhí)行
相關(guān)文章:
1. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. html5 - H5 audio 微信端 在IOS上不能播放音樂3. python - 編碼問題求助4. python - 我在使用pip install -r requirements.txt下載時(shí),為什么部分能下載,部分不能下載5. 視頻文件不能播放,怎么辦?6. mysql - 分庫(kù)分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來哪些效率或者其他方面的好處7. mysql - jdbc的問題8. python - Scrapy存在內(nèi)存泄漏的問題。9. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?10. mysql - 千萬級(jí)數(shù)據(jù)的表,添加unique約束,insert會(huì)不會(huì)很慢?
