javascript - 单击 parent.vue 中的按钮后触发 child.vue 中的功能
问题描述
我正在与BootstrapVue
.
我有一个方法,parent.vue
我将值 ( this.propsIndex
) 传递给我的child.vue
.
现在我想value
每次在我的 child.vue 的方法中单击它时都使用它 - 但是我怎样才能触发我的函数并使其工作呢?
非常感谢!
如果可能的话,我想避免使用watch
我的父母.vue
<template>
<div v-for="(id, index) in inputs" :key="index">
<b-button @click="deleteViaIndex(index)">Delete</b-button>
<child :indexProps="indexProps" />
</div>
<div>
<b-button @click="addInput()">Add Input</b-button>
</div>
</template>
<script>
methods: {
deleteViaIndex(index) {
this.propsIndex= index;
},
addInput() {
this.inputs.push({})
},
},
data() {
return {
inputs: [{}],
propsIndex: '',
}
}
</script>
我的孩子.vue(脚本)
props: ["propsIndex"],
methods: {
deleteViaParentIndex() {
//HERE I WANT TO USE IT EVERY TIME IT WILL BE CLICKED IN MY PARENT.VUE
//BUT FOR NOW IT'S NOT DOING ANYTHING WHEN I CONSOLE.LOG(this.propsIndex)
}
}
解决方案
看起来名字不匹配。在子组件中,您有一个 prop propsIndex
,但在您正在传递的父模板中indexProps
。
传递道具时,你必须记住,道具名称总是在第一部分,你传递的值应该放在下一个。https://vuejs.org/v2/guide/components-props.html#Passing-Static-or-Dynamic-Props
此外,由于 HTML 属性名称不区分大小写,因此您应该以这种方式传递道具:
<child :props-index="indexProps" />
推荐阅读
- sql - SQL 比较具有相似 ID 的行中的两个结果
- python - 在 pycharm ImportError: DLL load failed: 找不到指定的模块。导入人脸识别时
- c# - C# 数据表组,选择和求和
- ios - 如何使用 unarchivedObjectOfClass:fromData:error: 取消归档数据?
- java - 如何用 Jackson 解析大的嵌套 JSON 文件?
- dart - 如何在 Flutter 中将 Visual Studio Emulator 与 VScode 连接起来?
- c# - 创建一个循环,要求输入的第一个字母在上,其他字母在下
- javascript - 本地资产大量涌入
- r - 始终以分钟为单位获取时差
- algorithm - 使用 dincis 算法和 ford fulkerson 解决最大流量问题的最佳算法是什么?