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. python如何不改动文件的情况下修改文件的 修改日期2. node.js - node_moduls太多了3. angular.js - 不适用其他构建工具,怎么搭建angular1项目4. python - django 里自定义的 login 方法,如何使用 login_required()5. css3 - [CSS] 动画效果 3D翻转bug6. mysql优化 - mysql count(id)查询速度如何优化?7. angular.js - Angular路由和express路由的组合使用问题8. mysql主从 - 请教下mysql 主动-被动模式的双主配置 和 主从配置在应用上有什么区别?9. 主从备份 - 跪求mysql 高可用主从方案10. java8中,逻辑与 & 符号用在接口类上代表什么意思
![css3 - [CSS] 动画效果 3D翻转bug](http://www.haobala.com/attached/image/news/202304/110831f073.png)