我想通过插槽将一个按钮传递给组件,以便进行操作。例如,一个具有“添加行”功能的表格。
或者,这个被拆解的警告:
事实上,我想在组件定义内部将事件动作绑定到
我考虑使用指令来处理操作按钮,类似于这个库:https://bootstrap-vue.js.org/docs/components/modal,其中模态框按钮具有
更新: 我正在尝试使用指令来实现此功能。这是我目前的代码:https://jsfiddle.net/uvd6knLh/。
<my-table :data="data" :cols="cols">
<button slot="add_row"></button>
</my-table>
或者,这个被拆解的警告:
<alert>
<button slot="close">Remove the alert</button>
</alert>
以下是模板:
<div class="alert alert-info">
<slot></slot>
</div>
事实上,我想在组件定义内部将事件动作绑定到
<slot>
按钮,但仍然让组件用户提供自己的按钮(有时甚至是链接,<a>
元素)。我考虑使用指令来处理操作按钮,类似于这个库:https://bootstrap-vue.js.org/docs/components/modal,其中模态框按钮具有
v-b-modal
指令。但是我不知道如何实现。更新: 我正在尝试使用指令来实现此功能。这是我目前的代码:https://jsfiddle.net/uvd6knLh/。
Vue.component('alert', {
template: `
<div class="alert alert-info" v-if="show">
<slot></slot>
</div>`,
data() {
return {
show: true
}
},
mounted() {
// directive event
this.$root.$on("alert:close", () => this.show = false);
}
});
Vue.directive('alert-dismiss', {
inserted: function(el, binding, vnode) {
el.addEventListener("click", () => {
vnode.context.$root.$emit("alert:close");
});
}
});
所以我监听指令事件。问题在于,指令事件会导致所有警报组件关闭,正如您在jsfiddle中所看到的那样。 如何使这种模式起作用?
v-b-modal
指令的源代码在这里。 - Bert