vue公共组件

####实例1:按钮封装
正常思路是使用slot实现可复用组件(比如确定等字样的更改)
什么情况使用render?
示例:
export default {
name:’base-button’,//this will be shown in components tree
render(h) {
return h(‘a’,
{
class:[‘button’]
},
this.$slots.default)
}
}
子组件样式的slot配合props:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
<template>
<div :class="classes">
<slot>default</slot>
</div>
<template>
<script>
props:['type']
computed:{
classes(){
return {
'success':this.type==='success'
}
}
}
</script>
<style>
.success{

}
</style>

至于slot是按钮还是文本,就是在父组件中操作啦。
动态更新的话,要么在父组件中使用 :slot动态改变选择的slot的name,要么直接在公共组件中动态绑定name。
真*实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
<template>
<div>
<div id="modal" v-show='show'>
<p class="modal-topic"></p>
<span type="button" :class="btnStyle" @click="sure"></span>
<span type="button" @click="cancel"></span>
</div>
<div id="shadow" v-show="show"></div>
</div>
</template>
<script>
export default {
data(){
return {
btnStyle:'grey',//可props传递为动态
show:true
}
},
props:{//或者写成对象形式,使用computed得出该对象
topic:String,
btnText1:String,
btnText2:String
},
methods:{
sure(){
this.$emit('sure');
},
cancel(){
this.$emit('cancel');
}
}
}
</script>
<style>
.grey{
color:red;
}
#modal{
background-color:aquamarine;
width: 250px;
position: absolute;
top: 50%;
left:50%;
transform:translate(-50%,-50%);
box-shadow: 10px 10px 5px #888888;
padding:10px 0 20px 0;
z-index: 99;
border-radius: 10px;
}
#shadow{
position: fixed;
top: 0;
width: 100%;
height: 100%;
background-color: grey;
z-index:55;
opacity: 0.5;
filter: blur(25px);
}
span{
display: inline-block;
margin-top: 10px;
margin-bottom:0;
padding:5px 6px;
cursor: pointer;
box-sizing: border-box;
border: 1px solid transparent;
border-radius: 4px;
color: #333;
background-color: #fff;
border-color: #ccc;
}
</style>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
<template>
<div id="app">
<test :topic="topic" :btnText1="text1" :btnText2="text2" @sure="sure" @cancel="cancel" ref="test"></test>
</div>
</template>
<script>
export default{
name: 'app',
data () {
return {
topic:'您拨打的用户已关机,请稍后再拨您拨打的用户已关机,请稍后再拨您拨打的用户已关机,请稍后再拨',
text1:'sure',
text2:'cancel'
}
},
components:{
test
},
methods:{
sure(){
},
cancel(){
this.$refs.test.show = false;
}
}
}
</script>

注意遮罩层。
我们可能需要在一个嵌套了很多层的子组件里面触发modal。这种情况下,你应该把modal放在根组件里面,然后从子组件触发一个事件上去。
效果图:
""

####实例2:tab标签