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

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

javascript - 子組件觸發(fā)父組件的自定義事件 父組件無任何反應

瀏覽:175日期:2023-02-19 17:57:26

問題描述

以下為子組件 @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) 并未執(zhí)行

問題解答

回答1:

你直接這么寫有問題的吧應該是

<order-type @showChange=’alertFun’></order-type> 父組件有一個方法methods: { alertFun () {alert(2) }}

這里應該傳遞的是父組件方法的一個函數(shù)名,而不是直接寫alert(2)

回答2:

應該是這塊出問題了<option v-for='branch in branchList' :value='branch.id' track-by='$index'>for in對象循環(huán)取得的是索引,不是值,所以取不到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) 并未執(zhí)行

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 化隆| 双城市| 内江市| 南江县| 利津县| 荣成市| 雅江县| 兴宁市| 双牌县| 大庆市| 博乐市| 民丰县| 枞阳县| 榕江县| 通许县| 凉城县| 肇源县| 巴青县| 临江市| 团风县| 石景山区| 鄢陵县| 汤阴县| 宁德市| 莱西市| 林甸县| 吴桥县| 长子县| 石棉县| 当涂县| 紫阳县| 东乡县| 怀安县| 区。| 卓尼县| 杭锦旗| 大庆市| 肥乡县| 遂川县| 夏津县| 兴义市|