1
這可以起作用,但我確信有更好的方法來做到這一點。請看底部附近的document.getElementsByClassName()
部分。在Vue中添加「關閉模式」按鈕的更好的方法
HTML:
<modal>
<a slot="trigger">Show Modal</a>
<h3 slot="header">My Heading</h3>
<p slot="body">Here is the body of the modal.</p>
<div slot="footer">
Here is the footer, with a button to close the modal.
<button class="close-modal">Close Modal</button>
</div>
</modal>
Modal.vue:
<template>
<span>
<span @click="show = true">
<slot name="trigger"></slot>
</span>
<slot name="header"></slot>
<slot name="body"></slot>
<slot name="footer"></slot>
</span>
</template>
<script>
export default {
data() {
return { show: false }
},
mounted(that = this) {
document.getElementsByClassName('close-modal')[0].addEventListener('click', function() {
that.show = false;
})
}
}
</script>
有沒有更好的方式來做到這一點?
我想要將置於標記內,而不是在組件內部。這樣我可以用很多不同的方式關閉一個模態,但是隻有一個模態組件。 –
Travis
那麼它更容易,但你必須遵循相同的方法......我會加入到我的回答 – peaceman
@peaceman,也許你可以幫助我:https://stackoverflow.com/questions/45451971/how-can- I-顯示模態式模態上-VUE組分 –