javascript - 子组件触发父组件的自定义事件 父组件无任何反应
大家讲道理
大家讲道理 2017-07-05 11:03:26
0
3
939

以下为子组件 @change='showChange'为子组件事件
以下模板注册为 order-type组件

<template>
<select name="dType" class="form-control" 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) 并未执行

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(3)
女神的闺蜜爱上我

你直接这么写有问题的吧
应该是

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

这里应该传递的是父组件方法的一个函数名,而不是直接写alert(2)

女神的闺蜜爱上我

应该是这块出问题了
<option v-for="branch in branchList" :value="branch.id" track-by="$index">
for in对象循环取得的是索引,不是值,所以取不到branch.id,可以改成for of

伊谢尔伦

以下为子组件 @change='showChange'为子组件事件
以下模板注册为 order-type组件

<template>
<select name="dType" class="form-control" 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) 并未执行 
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!