尝试 运行 现有项目中的模态组件 VueJS
Trying to run Modal component VueJS in existing project
我做了一个单独的.vue文件,里面放了Modal Box的"template"和需要的CSS(稍后我会把它移到main.css)。我需要的功能是 - 在主页 Data.vue 上,客户单击按钮并打开模态框,he/she 可以在其中编写消息并将其发送给我们。
modalTest.vue
<template>
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-container">
<div class="modal-header">
<slot name="header">
default header
</slot>
</div>
<div class="modal-body">
<slot name="body">
default body
</slot>
</div>
<div class="modal-footer">
<slot name="footer">
default footer
<button class="modal-default-button" @click="$emit('close')">
OK
</button>
</slot>
</div>
</div>
</div>
</div>
</transition>
</template>
<style>
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .5);
display: table;
transition: opacity .3s ease;
}
.modal-wrapper {
display: table-cell;
vertical-align: middle;
}
.modal-container {
width: 300px;
margin: 0px auto;
padding: 20px 30px;
background-color: #fff;
border-radius: 2px;
box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
transition: all .3s ease;
font-family: Helvetica, Arial, sans-serif;
}
.modal-header h3 {
margin-top: 0;
color: #42b983;
}
.modal-body {
margin: 20px 0;
}
.modal-default-button {
float: right;
}
.modal-enter {
opacity: 0;
}
.modal-leave-active {
opacity: 0;
}
.modal-enter .modal-container,
.modal-leave-active .modal-container {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
</style>
我添加了我需要单击的按钮,以便在项目的一个页面中弹出模态框。
Data.vue
<template>
...
a lot of divs
...
<button id="show-modal" @click="showModal = true">Show Modal</button>
...
</template>
<script>
export default {
}
</script>
最后但同样重要的是
main.js
import modalTest from './components/modalTest'
Vue.component('modal', modalTest);
const app = new Vue({
el: '#app',
render: h => h(App),
router,
data: {
showModal: false
}
});
这就是我到现在为止设法做到的。但是,当我单击页面 Data.vue 中的按钮时,没有任何反应。查看控制台,没有关于该问题的信息。也许我误会了一些非常愚蠢的东西,但我现在找不到任何错误。
将 showModal: false 放入子页面数据的 return 中解决了问题:) 感谢@Vamsi Krishna
我做了一个单独的.vue文件,里面放了Modal Box的"template"和需要的CSS(稍后我会把它移到main.css)。我需要的功能是 - 在主页 Data.vue 上,客户单击按钮并打开模态框,he/she 可以在其中编写消息并将其发送给我们。
modalTest.vue
<template>
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-container">
<div class="modal-header">
<slot name="header">
default header
</slot>
</div>
<div class="modal-body">
<slot name="body">
default body
</slot>
</div>
<div class="modal-footer">
<slot name="footer">
default footer
<button class="modal-default-button" @click="$emit('close')">
OK
</button>
</slot>
</div>
</div>
</div>
</div>
</transition>
</template>
<style>
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .5);
display: table;
transition: opacity .3s ease;
}
.modal-wrapper {
display: table-cell;
vertical-align: middle;
}
.modal-container {
width: 300px;
margin: 0px auto;
padding: 20px 30px;
background-color: #fff;
border-radius: 2px;
box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
transition: all .3s ease;
font-family: Helvetica, Arial, sans-serif;
}
.modal-header h3 {
margin-top: 0;
color: #42b983;
}
.modal-body {
margin: 20px 0;
}
.modal-default-button {
float: right;
}
.modal-enter {
opacity: 0;
}
.modal-leave-active {
opacity: 0;
}
.modal-enter .modal-container,
.modal-leave-active .modal-container {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
</style>
我添加了我需要单击的按钮,以便在项目的一个页面中弹出模态框。
Data.vue
<template>
...
a lot of divs
...
<button id="show-modal" @click="showModal = true">Show Modal</button>
...
</template>
<script>
export default {
}
</script>
最后但同样重要的是 main.js
import modalTest from './components/modalTest'
Vue.component('modal', modalTest);
const app = new Vue({
el: '#app',
render: h => h(App),
router,
data: {
showModal: false
}
});
这就是我到现在为止设法做到的。但是,当我单击页面 Data.vue 中的按钮时,没有任何反应。查看控制台,没有关于该问题的信息。也许我误会了一些非常愚蠢的东西,但我现在找不到任何错误。
将 showModal: false 放入子页面数据的 return 中解决了问题:) 感谢@Vamsi Krishna