Skip to content

Commit a7a23d7

Browse files
committed
modify by dk
1 parent 44ca784 commit a7a23d7

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
- [样式绑定](https://github.com/dk-lan/vue-erp/tree/master/VueBasic/StyleBinding)
1818
- [Vue 实例化时基本属性](https://github.com/dk-lan/vue-erp/tree/master/VueBasic/VueBasicOptions)
1919
- [修饰符](https://github.com/dk-lan/vue-erp/tree/master/VueBasic/Modifiers)
20-
- 组件
21-
- 指令
20+
- [组件](https://github.com/dk-lan/vue-erp/tree/master/VueBasic/Component)
21+
- [指令](https://github.com/dk-lan/vue-erp/tree/master/VueBasic/TemplateSyntax#指令)
22+
- 自定义指令
2223
- 动画和过度效果
2324
- 路由
24-
- 钩子函数
2525
- Vuex
2626

2727
# 认识 Vue

VueBasic/Component/README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,55 @@
5454
<h1>局部组件</h1>
5555
</div>
5656
```
57+
## 组件是一个单独的作用域
58+
每个组件都有单独的作用域
59+
```html
60+
<div id="app">
61+
<p>{{count}}</p>
62+
<component1/>
63+
</div>
64+
```
65+
```javascript
66+
var vm = new Vue({
67+
el: '#app',
68+
data: {
69+
count: 10
70+
},
71+
methods: {
72+
increment: function(){
73+
this.count += 1;
74+
}
75+
},
76+
components:{
77+
'component1': {
78+
template: '<button v-on:click="increment">{{ count }}</button>',
79+
data: function(){
80+
//在组件里面 data 一定是 function 并返回一个对象
81+
return {
82+
count: 0
83+
}
84+
},
85+
methods: {
86+
increment: function(){
87+
this.count += 1;
88+
}
89+
}
90+
}
91+
}
92+
})
93+
```
94+
渲染结果为
95+
```html
96+
<div id="app">
97+
<p>10</p>
98+
<!--
99+
此按钮每次点击都会自增 1,而 p 标签永远都是为 10
100+
原因为组件的作用域是单独的
101+
-->
102+
<button>0</button>
103+
</div>
104+
```
105+
[效果预览](https://dk-lan.github.io/vue-erp/VueBasic/Component/component.html)
57106

58107
## 特殊的 HTML 结构中使用 is
59108
比如在下拉列表(select)元素里面,子元素必须为 option,则在使用组件的时候用 is
@@ -152,7 +201,8 @@
152201
```
153202

154203
## slot 分发内容
155-
Vue 组件默认是覆盖渲染
204+
Vue 组件默认是覆盖渲染,为了解决这一问题,Vue 提出了 slot 分发内容
205+
156206
```html
157207
<div id="app">
158208
<component1>
@@ -176,12 +226,19 @@ Vue 组件默认是覆盖渲染
176226
<div id="app">
177227
<component1>
178228
<h1>Tom</h1>
229+
<!--
230+
如果在组件定义的 template 当中没有 <slot></slot>,那么下面两个 h1 标签将不会存在
231+
换句话说就是 <slot></slot> = <h1>Sam</h1><h1>Lucy</h1>
232+
<slot></slot>可以放到 <h1>Tom</h1> 上面进行位置调换
233+
-->
179234
<h1>Sam</h1>
180235
<h1>Lucy</h1>
181236
</component1>
182237
</div>
183238
```
239+
184240
### 具名 slot
241+
如果要将组件里面不同的子元素放到不同的地方,那就为子元素加上一个属性 slot="名称",然后在组件定义的时候用名称对应位置 <slot name="名称"></slot>,其它没有 slot 属性的子元素将统一分发到 <slot></slot> 里面
185242
```html
186243
<div id="app">
187244
<component1>
@@ -205,8 +262,10 @@ Vue 组件默认是覆盖渲染
205262
```html
206263
<div id="app">
207264
<component1>
265+
<!--<slot name="lucy"></slot> = <h1 slot="lucy">Lucy</h1>-->
208266
<h1>Lucy</h1>
209267
<h1>Tom</h1>
268+
<!--其它没有 slot 属性的子元素将全部分发到 <slot></slot> 标签-->
210269
<h1>Sam</h1>
211270
</component1>
212271
</div>

VueBasic/Component/component.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>组件</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<script type="text/javascript" src="../js/vue.min.js"></script>
8+
</head>
9+
<body>
10+
<div id="app">
11+
<fieldset>
12+
<legend><h3>组件单独作用域</h3></legend>
13+
<p>{{count}}</p>
14+
<component1/>
15+
</fieldset>
16+
</div>
17+
18+
<script type="text/javascript">
19+
var vm = new Vue({
20+
el: '#app',
21+
data: {
22+
count: 10
23+
},
24+
methods: {
25+
increment: function(){
26+
this.count += 1;
27+
}
28+
},
29+
components:{
30+
'component1': {
31+
template: '<button v-on:click="increment">{{ count }}</button>',
32+
data: function(){
33+
//在组件里面 data 一定是 function 并返回一个对象
34+
return {
35+
count: 0
36+
}
37+
},
38+
methods: {
39+
increment: function(){
40+
this.count += 1;
41+
}
42+
}
43+
}
44+
}
45+
})
46+
</script>
47+
</body>
48+
</html>

0 commit comments

Comments
 (0)