9
9
10
10
### [ Single File Component (a.k.a. SFC)] ( https://vuejs.org/v2/guide/single-file-components.html ) - Most Common
11
11
12
- ``` vue
13
- <template>
14
- <p class="demo">
15
- <button class="btn-primary" @click.prevent="handleClick">
16
- <slot></slot>(clicked - {{count}})
17
- </button>
18
- </p>
19
- </template>
20
-
21
- <script>
22
- export default {
23
- data() {
24
- return {
25
- count: 0,
26
- };
27
- },
28
- methods: {
29
- handleClick() {
30
- this.count++;
31
- console.log('clicked');
32
- },
33
- },
34
- };
35
- </script>
36
-
37
- <style scoped>
38
- .btn-primary {
39
- display: inline-block;
40
- font-size: 1.2rem;
41
- color: #fff;
42
- background-color: #3eaf7c;
43
- padding: 0.8rem 1.6rem;
44
- border-radius: 4px;
45
- transition: background-color 0.1s ease;
46
- box-sizing: border-box;
47
- border-bottom: 1px solid #389d70;
48
- }
49
- </style>
50
- ```
12
+ <<< @/docs/.vuepress/components/SFCButton.vue
51
13
52
14
<SFCButton >SFC</SFCButton >
53
15
@@ -57,7 +19,7 @@ export default {
57
19
Vue .component (' my-btn' , {
58
20
template: `
59
21
<button class="btn-primary" @click.prevent="handleClick">
60
- {{text}}
22
+ <slot></slot>(clicked - {{count}})
61
23
</button>
62
24
` ,
63
25
data () {
@@ -66,8 +28,16 @@ Vue.component('my-btn', {
66
28
};
67
29
},
68
30
methods: {
69
- handleClick () {
70
- console .log (' clicked' );
31
+ data () {
32
+ return {
33
+ count: 0 ,
34
+ };
35
+ },
36
+ methods: {
37
+ handleClick () {
38
+ this .count ++ ;
39
+ console .log (' clicked' , this .count );
40
+ },
71
41
},
72
42
},
73
43
});
@@ -77,14 +47,17 @@ Vue.component('my-btn', {
77
47
78
48
``` js
79
49
Vue .component (' my-btn' , {
80
- data () {
81
- return {
82
- text: ' Click me' ,
83
- };
84
- },
85
50
methods: {
86
- handleClick () {
87
- console .log (' clicked' );
51
+ data () {
52
+ return {
53
+ count: 0 ,
54
+ };
55
+ },
56
+ methods: {
57
+ handleClick () {
58
+ this .count ++ ;
59
+ console .log (' clicked' , this .count );
60
+ },
88
61
},
89
62
},
90
63
render (h ) {
@@ -98,7 +71,7 @@ Vue.component('my-btn', {
98
71
click: this .handleClick ,
99
72
},
100
73
},
101
- this .text
74
+ this .$slots . default
102
75
);
103
76
},
104
77
});
@@ -120,8 +93,8 @@ Vue.component('my-btn', {
120
93
},
121
94
render () {
122
95
return (
123
- < button class = " btn-primary" onClick = { this . handleClick } >
124
- {{ this .text }}
96
+ < button class = " btn-primary" @ click . prevent = " handleClick" >
97
+ {this .$slots . default }(clicked - {{count}})
125
98
< / button>
126
99
);
127
100
},
@@ -133,7 +106,7 @@ Vue.component('my-btn', {
133
106
``` vue
134
107
<template>
135
108
<button class="btn-primary" @click.prevent="handleClick">
136
- {{text}}
109
+ <slot></slot>(clicked - {{count}})
137
110
</button>
138
111
</template>
139
112
@@ -143,10 +116,11 @@ import Component from 'vue-class-component';
143
116
144
117
@Component
145
118
export default MyBtn extends Vue {
146
- text = 'Click me' ;
119
+ count = 0 ;
147
120
148
121
handleClick() {
149
- console.log('clicked');
122
+ this.count++;
123
+ console.log('clicked', this.count);
150
124
}
151
125
}
152
126
</script>
0 commit comments