<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="http://cdn.bootcss.com/jquery/2.1.1-rc2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://weui.github.io/weui/weui.css">
<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;
}
/*
* the following styles are auto-applied to elements with
* v-transition="modal" when their visiblity is toggled
* by Vue.js.
*
* You can easily play with the modal transition by editing
* these styles.
*/
.modal-enter, .modal-leave {
opacity: 0;
}
.modal-enter .modal-container,
.modal-leave .modal-container {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
</style>
</head>
<body>
<script type="x/template" id="modal-template">
<div class="modal-mask" v-show="show" transition="modal">
<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="show=false">OK</button>
</slot>
</div>
</div>
</div>
</div>
</script>
<div id="app">
<button class="show-modal" @click="showModal = true">Show Modal</button>
<modal :show.sync="showModal">
<h3 slot="header">custom he<strong>ader</strong></h3>
<h3 slot="body">custom body</h3>
<h3 slot="footer">custom footer</h3>
</modal>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.20/vue.min.js"></script>
<script type="text/javascript">
Vue.component(‘modal‘,{
template: ‘#modal-template‘,
props: {
show: {
type: Boolean,
required: true,
twoWay: true
}
}
})
new Vue({
el: ‘#app‘,
data: {
showModal: false
}
})
</script>
</body>
</html>
<slot>标签为一个插槽,它是一个可以负责内容分发的。
它的name属性是内容替换插槽的一个标志,插槽是会被替换掉的不会再页面显示。
@click="showModal = true" @click是一个事件绑定的缩写,直接给组件的props的showModal显示。
组件里的props的数据定义show: {type: Boolean,required: true,twoWay: true},type是类型的意思定义参数的类型 Boolean是布尔值的意思 required是必须的意思 twoWay是向vue实例写回数据。
:show.sync="showModal" 组件默认是单向数据绑定,加上.sync就是双向绑定。
v-show="show" transition="modal" 动态显示其属性。