8.重用组件

8.重用组件

案例

子组件

编写了一些css样式

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
75
<template>

<div class="button" :class="[type, size]">
<!-- 插槽,会将父标签的本文填充到里面 -->
<slot></slot>
</div>
</template>

<script>
const options = {
// 父组件使用时可以添加的属性
props: ["type", "size"]
}
export default options;
</script>

<!-- 使用scoped可以让CSS样式只对局部元素生效 -->
<style scoped>
.button {
display: inline-block;
text-align: center;
border-radius: 30px;
margin: 5px;
font: bold 12px/25px Arial, sans-serif;
padding: 0 2px;
text-shadow: 1px 1px 1px rgba(255, 255, 255, .22);
box-shadow: 1px 1px 1px rgba(0, 0, 0, .29), inset 1px 1px 1px rgba(255, 255, 255, .44);
transition: all 0.15s ease;
}

.button:hover {
box-shadow: 1px 1px 1px rgba(0, 0, 0, .29), inset 1px 1px 2px rgba(0, 0, 0, .5);
}

.button:active {
box-shadow: inset 1px 1px 2px rgba(0, 0, 0, .8);
}

.primary {
background-color: #1d6ef9;
color: #b5e3f1;
}

.danger {
background-color: rgb(196, 50, 50);
color: white;
}

.success {
background-color: #a5cd4e;
;
color: #3e5706;
}

.small {
width: 40px;
height: 20px;
font-size: 10px;
line-height: 20px;
}

.middle {
width: 50px;
height: 25px;
font-size: 14px;
line-height: 25px;
}

.large {
width: 60px;
height: 30px;
font-size: 18px;
line-height: 30px;
}
</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
29
30
31
32
<template>
<div>
<h1>父组件</h1>
<!-- 使用子组件 -->
<my-button type="primary" size="small">1</my-button>
<my-button type="danger" size="middle">2</my-button>
<my-button type="success" size="large">3</my-button>
</div>
</template>

<script>
// 导入子组件
import myButton from '../components/MyButton.vue'
const options = {
// 添加要使用的子组件
components: {
myButton
}
}
export default options
</script>

<style>
div {
background-color: gainsboro;
widows: 100px;
height: 400px;
}
h1 {
font-family: 华文行楷;
}
</style>

8.重用组件
http://xwww12.github.io/2022/10/06/前端/vue/8.重用组件/
作者
xw
发布于
2022年10月6日
许可协议