forked from sunseekers/Vue2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemit2.html
55 lines (55 loc) · 1.28 KB
/
emit2.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>emit2</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<div id='emit'>
<span >
{{date}}
<data-msg></data-msg>
</span>
</div>
<script type="text/javascript" src='js/vue.min.js'></script>
<script type="text/javascript">
//准备一个空的实例对象
var Event=new Vue();
new Vue({
el:'#emit',
data:{
date:'2018.01.07,我是父组件'
},
components:{
'data-msg':{
//data 必须是函数,所以 需要有返回值
data(){
return {msg:'我是子组件'}
},
template:`
<div><span @click='send'>{{msg}}</span></div>
`,
methods:{
send(){
//Event.$emit(事件名称,数据);
Event.$emit('msg',this.msg);
}
}
}
},
mounted(){
//Event.$on(事件名称,function(data){}.bind(this))
var self=this;
Event.$on('msg',function(c){
self.date=c;
console.log(c);
})
}
})
</script>
</body>
</html>