您可以根据自己的具体需求创建一个组件。像这样的事情应该让你开始。
<template>
<div class="modal" v-if="show">
<h3>{{title}}</h3>
<div class="content" v-html="content">
<!-- Remove this if you don't need complex content -->
<slot></slot>
</div>
<div role="group">
<button @click="submit">Save</button>
<button @click="close">Cancel</button>
</div>
</div>
</template>
<script>
export default {
name: 'Modal',
data() {
return {
show: false,
title: '',
content: '' // <- Accepts both HTML and plain text (as message)
}
},
methods: {
toggle(state) {
this.show = state;
},
open(title, content) {
this.title = title;
this.content = content;
this.toggle(true);
},
close() {
this.toggle(false);
},
submit() {
// Handle form submission here
this.close();
}
}
}
</script>
JS
import Modal from './components/Modal.vue';
const vm = new Vue({
methods: {
editItem() {
this.$refs.modal.open('Title', 'Content here');
}
},
components: {
Modal
}
})
HTML
<table>
<!-- rows and stuff -->
</table>
<!-- Add a `ref` so that it can be referenced later -->
<modal ref="modal"></modal>
<button @click.prevent="editItem">Edit item</button>