javascript - 子組件觸發父組件的自定義事件 父組件無任何反應
問題描述
以下為子組件 @change=’showChange’為子組件事件以下模板注冊為 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) 并未執行
問題解答
回答1:你直接這么寫有問題的吧應該是
<order-type @showChange=’alertFun’></order-type> 父組件有一個方法methods: { alertFun () {alert(2) }}
這里應該傳遞的是父組件方法的一個函數名,而不是直接寫alert(2)
回答2:應該是這塊出問題了<option v-for='branch in branchList' :value='branch.id' track-by='$index'>for in對象循環取得的是索引,不是值,所以取不到branch.id,可以改成for of
回答3:以下為子組件 @change=’showChange’為子組件事件以下模板注冊為 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) 并未執行
相關文章:
1. javascript - 從mysql獲取json數據,前端怎么處理轉換解析json類型2. python - Scrapy如何得到原始的start_url3. python小白 關于類里面的方法獲取變量失敗的問題4. 求救一下,用新版的phpstudy,數據庫過段時間會消失是什么情況?5. thinkPHP5中獲取數據庫數據后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙6. mysql - ubuntu開啟3306端口失敗,有什么辦法可以解決?7. django - Python error: [Errno 99] Cannot assign requested address8. android - 安卓做前端,PHP做后臺服務器 有什么需要注意的?9. linux運維 - python遠程控制windows如何實現10. extra沒有加載出來
