Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2bc62f1

Browse files
committed
upload vuejs first
1 parent 98558d0 commit 2bc62f1

8 files changed

+2989
-0
lines changed

vuejs/lesson1.md

+895
Large diffs are not rendered by default.

vuejs/unit2.md

+821
Large diffs are not rendered by default.

vuejs/unit3-component.md

+317
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
### Vue.js 组件
2+
1. 定义组件的方式
3+
4+
5+
方式1:先创建组件构造器,然后由组件构造器创建组件
6+
7+
var cp = Vue.extend({template:'组件元素内容'})
8+
Vue.component('组件名',cp)
9+
方式2:直接创建组件(推荐)
10+
11+
Vue.component('组件名',{template:'组件元素内容'})
12+
13+
因为组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 data、computed、watch、methods 以及生命周期钩子等。仅有的例外是像 el 这样根实例特有的选项。
14+
15+
##demo:
16+
17+
<template id="wbs">
18+
<div>
19+
<h3>{{msg}}</h3>
20+
<ul>
21+
#注意这里的$event,表示事件对象
22+
<li v-for="item in list" @click="display($event)">{{item.title}}</li>
23+
</ul>
24+
</div>
25+
</template>
26+
<!--行为层-->
27+
<script type="text/javascript">
28+
// #定义组件
29+
Vue.component('job-item', {
30+
props: ['list','msg'],
31+
template: '#wbs',
32+
//组件内部的函数
33+
methods:{
34+
display:function (t) {
35+
alert(t.target.innerText)
36+
}
37+
}
38+
})
39+
40+
var mydata = {
41+
title: "vue.js",
42+
jobs: [
43+
{"id": "001", "title": "python", "salary": 8000},
44+
{"id": "002", "title": "web", "salary": 9000},
45+
{"id": "003", "title": "js", "salary": 10000},
46+
{"id": "004", "title": "spider", "salary": 10000}
47+
],
48+
works: [
49+
{"id": "001", "title": "语文", "salary": 8000},
50+
{"id": "002", "title": "书序", "salary": 9000},
51+
{"id": "003", "title": "英语", "salary": 10000},
52+
{"id": "004", "title": "化学", "salary": 10000}
53+
]
54+
}
55+
var app = new Vue({
56+
// el:element
57+
el: '#myapp',
58+
data: mydata,
59+
components: {
60+
'my-tab': {
61+
template: '<h2 v-text="content"></h2>',
62+
data: function () {
63+
return {
64+
content: 0
65+
}
66+
}
67+
}
68+
},
69+
methods: {
70+
changeData: function () {
71+
}
72+
}
73+
})
74+
75+
76+
</script>
77+
2. 组件的分类
78+
79+
分类:全局组件、局部组件
80+
81+
components:{ //局部组件
82+
'my-world':{
83+
template:'<h3>{{age}}</h3>',
84+
data:function (){
85+
return {
86+
age:25
87+
}
88+
}
89+
}
90+
}
91+
92+
3. 引用模板
93+
94+
将组件元素内容放到模板\<template>中并引用其id
95+
96+
97+
<template id="wbs">
98+
<div>
99+
<h3>{{msg}}</h3>
100+
<ul>
101+
<li v-for="value in arr">{{value}}</li>
102+
</ul>
103+
</div>
104+
</template>
105+
components:{
106+
'my-hello':{
107+
template:'#wbs',
108+
}
109+
}
110+
111+
这个组件需要传递两个参数,调用方式为
112+
113+
<my-hello :msg='var' :arr='array'></my-hello>
114+
4. 动态组件
115+
116+
<component :is="">组件
117+
多个组件使用同一个挂载点,然后动态的在它们之间切换
118+
119+
<keep-alive>组件,使用缓存数据,适用于非活动组件
120+
121+
122+
<keep-alive>
123+
<component :is="flag"></component>//使该组件不每次请求,采用缓存数据
124+
<keep-alive>
125+
126+
5. 父子组件
127+
128+
129+
在一个组件内部定义另一个组件,称为父子组件
130+
131+
子组件只能在父组件内部使用
132+
133+
默认情况下,子组件无法访问父组件中的数据,每个组件实例的作用域是独立的
134+
135+
```
136+
var vm = new Vue({
137+
el:'#d1',
138+
data:{msg:'hello'},
139+
components:{
140+
'my-world':{
141+
template:'#fatherComp',
142+
components:{
143+
'my-hello':{
144+
template:'#sonComp'
145+
}
146+
},
147+
}
148+
}
149+
})
150+
<template id='fatherComp'><div><h1>父组件</h1><my-hello></my-hello></div></template>
151+
<template id='sonComp'><div><h2>子组件</h2></div></template>
152+
```
153+
6. 组件间数据传递 (通信)
154+
155+
#### 6.1 子组件访问父组件的数据
156+
157+
a) 在调用子组件时,绑定想要获取的父组件中的数据
158+
159+
b) 在子组件内部,使用props选项声明获取的数据,即接收来自父组件的数据
160+
161+
总结:父组件通过props向下传递数据给子组件
162+
163+
注:props除了字符串数组之外,也可以采用对象类型,允许配置高级设置,如类型判断、数据校验、设置默认值
164+
165+
166+
props:{
167+
message:String,
168+
name:{
169+
type:String,
170+
required:true
171+
},
172+
age:{
173+
type:Number,
174+
default:18,
175+
validator:function(value){
176+
return value>=0;
177+
}
178+
},
179+
user:{
180+
type:Object,
181+
default:function(){ //对象或数组的默认值必须使用函数的形式来返回
182+
return {id:3306,username:'秋香'};
183+
}
184+
}
185+
},
186+
187+
188+
#### 6.2 父组件访问子组件的数据
189+
190+
a) 在子组件中使用vm.$emit(事件名,数据)触发一个自定义事件,事件名自定义
191+
192+
193+
//子组件内部定义发送方法
194+
methods:{
195+
send(){
196+
this.$emit('e-hello',this.sname,this.sage)
197+
}
198+
}
199+
200+
b) 父组件在使用子组件的地方监听子组件触发的事件,并在父组件中定义方法,用来获取数据
201+
202+
<template id='fatherComp'>
203+
<div>
204+
<h1>父组件数据{{fname}},获取子组件数据{{fsname}}-{{fsage}}</h1>
205+
<my-hello :name=fname :age=fage @e-hello="getdata"></my-hello>
206+
</div>
207+
</template>
208+
209+
总结:子组件通过events给父组件发送消息,实际上就是子组件把自己的数据发送到父组件
210+
211+
#### 6.3 单向数据流
212+
props是单向绑定的,当父组件的属性变化时,将传导给子组件,但是不会反过来
213+
而且不允许子组件直接修改父组件中的数据,会报错
214+
解决方式:
215+
方式1:如果子组件想把它作为局部数据来使用,可以将数据存入另一个变量中再操作,不影响父组件中的数据
216+
方式2:如果子组件想修改数据并且同步更新到父组件,两个方法:
217+
a.使用.sync(1.0版本中支持,2.0版本中不支持,2.3版本又开始支持)
218+
需要显式地触发一个更新事件
219+
b.可以将父组件中的数据包装成对象,然后在子组件中修改对象的属性(因为对象是引用类型,指向同一个内存空间),推荐
220+
221+
### 7. 非父子组件间的通信
222+
非父子组件间的通信,可以通过一个空的Vue实例作为中央事件总线(事件中心),用它来触发事件和监听事件
223+
224+
var Event=new Vue();
225+
//发送数据组件
226+
var A={
227+
template:'#a',
228+
data(){
229+
return {
230+
name:'tom'
231+
}
232+
},
233+
methods:{
234+
send(){
235+
Event.$emit('data-a',this.name);
236+
}
237+
}
238+
}
239+
//接收数据组件
240+
var C={
241+
template:'#c',
242+
data(){
243+
return {
244+
name:'',
245+
age:''
246+
}
247+
},
248+
mounted(){ //在模板编译完成后执行
249+
Event.$on('data-a',name => {
250+
this.name=name;
251+
// console.log(this);
252+
});
253+
254+
Event.$on('data-b',age => {
255+
this.age=age;
256+
});
257+
}
258+
}
259+
260+
261+
262+
263+
264+
265+
266+
267+
268+
269+
270+
271+
272+
273+
274+
275+
276+
277+
278+
279+
280+
281+
282+
283+
284+
285+
286+
287+
288+
289+
290+
291+
292+
293+
294+
295+
296+
297+
298+
299+
300+
301+
302+
303+
304+
305+
306+
307+
308+
309+
310+
311+
312+
313+
314+
315+
316+
317+

0 commit comments

Comments
 (0)