gpt4 book ai didi

methods - vue2 使用 $emit 从组件调用父函数

转载 作者:行者123 更新时间:2023-12-01 09:15:43 24 4
gpt4 key购买 nike

我试图从子组件调用父方法,但它似乎不起作用..这里的代码:

index.html

<div class="percorso"v-on:removeall="pathlengthTozero()">
</div>

组件
Vue.component('lista-percorso', {
template:`
<div class="col-lg-2 col-xs-2">
<div class="removeall pull-right" v-on:click="removeall()"></div>
</div>`,

methods:{
removeall : function(){
this.listaSelezionati = [];
this.$emit('removeall');
}
}

父方法
    pathlengthTozero : function(){
il_tuo_percorso = [''];
}

似乎没有在发出时调用“pathlengthTozero”,这是使用它的正确方法吗?

最佳答案

你需要把这个v-on:removeall="pathlengthTozero"到组件 <lista-percorso>像下面,

<lista-percorso v-on:removeall="pathlengthTozero"></lista-percorso>

this.$emit将能够触发父方法。

示例演示:

Vue.component('lista-percorso', {
template:`
<div class="col-lg-2 col-xs-2">
<div class="removeall pull-right" v-on:click="removeall()">xxxxxxxxxx</div>
</div>`,

methods:{
removeall : function(){
this.listaSelezionati = [];
this.$emit('removeall');
}
}
})



var App = new Vue({
el: '#app',
methods:{
pathlengthTozero : function(){
alert('hello');
il_tuo_percorso = [''];
}
}
});
<script src="https://unpkg.com/vue"></script>

<div id="app">
<div class="percorso" ></div>

<lista-percorso v-on:removeall="pathlengthTozero"></lista-percorso>
</div>

关于methods - vue2 使用 $emit 从组件调用父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45371435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com