Javascript Vue2从循环组件中的子级调用父方法
我正在循环根组件,它具有with child组件Javascript Vue2从循环组件中的子级调用父方法,javascript,vue.js,vuejs2,vue-component,event-bus,Javascript,Vue.js,Vuejs2,Vue Component,Event Bus,我正在循环根组件,它具有with child组件 <root-select v-for="offer in offers" > <child-options v-for="item in options" > </child-options> </root-select> 根目录: 但我需要的是,数据只会在触发组件中更改 <root-select v-for="offer in offers" > <chi
<root-select v-for="offer in offers" >
<child-options v-for="item in options" >
</child-options>
</root-select>
根目录:
但我需要的是,数据只会在触发组件中更改
<root-select v-for="offer in offers" >
<child-options v-for="item in options" >
</child-options>
</root-select>
谢谢。尽量不要使用事件总线发出。使用普通发射方式从子级发射到父级 在子组件函数中:
this.$emit('toggle', value);
<template><child-options v-for="item in options" @toggle="onToggleFn"></child-options></template>
<script>
...
methods:{
onToggleFn:function(){
//your logic here
}
}
</script>
在父组件函数中:
this.$emit('toggle', value);
<template><child-options v-for="item in options" @toggle="onToggleFn"></child-options></template>
<script>
...
methods:{
onToggleFn:function(){
//your logic here
}
}
</script>
...
方法:{
onToggleFn:function(){
//你的逻辑在这里
}
}
尽量不要使用事件总线发出。使用普通发射方式从子级发射到父级
在子组件函数中:
this.$emit('toggle', value);
<template><child-options v-for="item in options" @toggle="onToggleFn"></child-options></template>
<script>
...
methods:{
onToggleFn:function(){
//your logic here
}
}
</script>
在父组件函数中:
this.$emit('toggle', value);
<template><child-options v-for="item in options" @toggle="onToggleFn"></child-options></template>
<script>
...
methods:{
onToggleFn:function(){
//your logic here
}
}
</script>
...
方法:{
onToggleFn:function(){
//你的逻辑在这里
}
}
this.toggle函数的作用是什么?为什么要使用事件总线而不是监听组件上的事件?Karl AndréGagnon,你的意思是这样的。$emit('toggle',value)?不,我的意思是像
一样,你的切换函数看起来像(项,值)->项=值代码>。当然,我假设toggle
做的不仅仅是赋值,否则@toggle=“item=$event.value”
就足够了。我已经有一段时间没有接触Vue了,所以可能不准确,但是逻辑就在这里。this.toggle函数做什么?为什么要使用EventBus
而不是监听组件上的事件?Karl AndréGagnon,你的意思是这样的。$emit('toggle',value)?不,我的意思是像
,而你的toggle函数看起来像(item,value)->item=value代码>。当然,我假设toggle
做的不仅仅是赋值,否则@toggle=“item=$event.value”
就足够了。我已经有一段时间没有接触Vue了,所以它可能不准确,但逻辑就在这里。您的答案简单明了。谢谢你。你的回答简单明了。非常感谢。