Vue你不知到的$this.emit()的用法
需求


需求:除了拿到false,还要拿到v-for中的index
如何解决:再使用一次父传子,:a="index" 将下标值传递给子组件 注意要加引号
<experts @handlerchange="ChangeV" :a="index"></experts>
在html页面直接{{a}}就可以使用props中的值,
在methods:{}中要通过this哦
this.$emit("handlerchange", [this.a, false]);
this.$emit()中的传递多个参数使用中括号哦
子组件
<template>
<!-- 子组件 -->
<div>
<h2 @click="getcon">123--{{a}}</h2>
</div>
</template>
<script>
export default {
props: ["a"],
methods: {
getcon() {
this.$emit("handlerchange", [this.a, false]);
}
}
};
</script>
父组件
<template>
<div>
<div v-for="(item,index) in arr" :key="index" class="box">
<h2>标题</h2>
<p>{{item.name}}</p>
<experts @handlerchange="ChangeV" :a="index"></experts>
</div>
</div>
</template>
<script>
import experts from "../../../components/myExperts";
export default {
data() {
return {
arr: [
{ name: "张三", id: "1" },
{ name: "张四", id: "2" },
{ name: "王五", id: "3" }
]
};
},
components: {
experts
},
methods: {
ChangeV(meass) {
console.log(meass);
}
}
};
</script>
<style scoped>
.box {
margin-top: 20px;
}
</style>

遇见问题,这是你成长的机会,如果你能够解决,这就是收获。
作者:晚来南风晚相识
出处:https://www.cnblogs.com/IwishIcould/
本文版权归作者所有,欢迎转载,未经作者同意须保留此段声明,在文章页面明显位置给出原文连接
如果文中有什么错误,欢迎指出。以免更多的人被误导。
出处:https://www.cnblogs.com/IwishIcould/
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,或者关注博主,在此感谢!
万水千山总是情,打赏5毛买辣条行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主(っ•̀ω•́)っ✎⁾⁾!
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!
支付宝
微信
如果文中有什么错误,欢迎指出。以免更多的人被误导。

浙公网安备 33010602011771号