javascript - 如何从开槽组件调用方法或向父组件发出事件?
问题描述
这是我的模板
<template>
<some-component>
<button>Hello!</button>
</some-component>
</template>
这是我的组件(一些组件)
<template>
<slot />
</template>
<script>
export default {
methods: {
someMethod() {
// Does something
}
}
}
</script>
我将如何someMethod()
从插入组件的按钮调用,或者发出一个some-component
可以监听的事件,以从插入的按钮运行方法本身?
解决方案
someMethod
您可以作为插槽道具传递some-component
:
<template>
<slot :onClick="someMethod" />
</template>
然后使用的父级some-component
可以将插槽道具绑定到button
:
<some-component>
<template v-slot="{ onClick }">
<button @click="onClick">Click me</button>
</template>
</some-component>
推荐阅读
- r - 在 r 中绑定或合并多个 powerpoint
- firebase - 如何解决与firebase的连接
- python - 递归如何在python内部工作(递归的源代码)?
- reactjs - 如何在reactjs中更改输入类型=“文件”中的默认文本?
- python - PySpark 和 argparse
- node.js - 使用 node.js 在 Catalina 上的 OpenSSL 问题
- azure-pipelines - 将变量传递给 Azure YAML 管道中的模板
- python - 将 igraph 用于 Graphml 网络
- r - 为什么 Tidyeval 引用在 lamdas 中失败?
- c# - 无法在家庭控制器中使用模型类方法