标题几乎解释了一切。我已经在 StackOverflow 上搜索了解决方案,但他们都没有 帮助。
使用经典模态组件的示例:
Vue.component('modal', {
template: `
<transition name="modal">
<div class="modal-wrapper">
<div class="modal-dialog">
<slot></slot>
</div>
</div>
</transition>
`,
})
const app = new Vue({
el: '#app',
data: {
showModal: false,
},
})
/* transition */
.modal-enter-active,
.modal-leave-active {
transition: opacity .5s;
}
.modal-enter,
.modal-leave-to {
opacity: 0;
}
.modal-wrapper {
position: absolute;
left: 0; right: 0;
top: 0; bottom: 0;
display: flex;
justify-content: center;
align-items: center;
background: rgba(0, 0, 0, .25);
}
.modal-dialog {
max-width: 90%;
padding: 1em;
background: white;
}
<script src="https://vuejs.org/js/vue.js"></script>
<div id="app">
<p><button @click="showModal = true">Show modal</button></p>
<modal v-if="showModal">
<h3>Hello world</h3>
<p>Amet quam alias amet incidunt voluptatum sapiente Mollitia</p>
<p><button @click="showModal = false">Close</button></p>
</modal>
</div>
(控制台中也没有错误)
但是,使用时一切正常v-show
。但我不能在我的项目中真正使用它来代替 v-if 。
Vue.component('modal', {
template: `
<transition name="modal">
<div class="modal-wrapper">
<div class="modal-dialog">
<slot></slot>
</div>
</div>
</transition>
`,
})
const app = new Vue({
el: '#app',
data: {
showModal: false,
},
})
/* transition */
.modal-enter-active,
.modal-leave-active {
transition: opacity .5s;
}
.modal-enter,
.modal-leave-to {
opacity: 0;
}
.modal-wrapper {
position: absolute;
left: 0; right: 0;
top: 0; bottom: 0;
display: flex;
justify-content: center;
align-items: center;
background: rgba(0, 0, 0, .25);
}
.modal-dialog {
max-width: 90%;
padding: 1em;
background: white;
}
<script src="https://vuejs.org/js/vue.js"></script>
<div id="app">
<p><button @click="showModal = true">Show modal</button></p>
<modal v-show="showModal">
<h3>Hello world</h3>
<p>Amet quam alias amet incidunt voluptatum sapiente Mollitia</p>
<p><button @click="showModal = false">Close</button></p>
</modal>
</div>
也就是说,我必须在使用模态的所有地方进行包装<modal>
,并从模态本身中删除过渡(这听起来不太好)<transition>
<transition name="modal">
<modal>
...
</modal>
</transition>
为什么会这样以及如何使输入动画起作用(使用v-if
, 和<transition>
在模态组件中?
我注意到 Vue 2.5(而不是 Vue 2.6)没有这样的问题。从那以后肯定发生了一些变化。