-
Notifications
You must be signed in to change notification settings - Fork 1
/
06.html
59 lines (55 loc) · 1.14 KB
/
06.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
56
57
58
59
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>单向数据流</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="itany">
<h2>父组件:{{name}}</h2>
<input type="text" v-model="name">
<h2>父组件:{{user.age}}</h2>
<hr>
<my-hello :name.sync="name" :user="user"></my-hello>
</div>
<template id="hello">
<div>
<h3>子组件:{{name}}</h3>
<h3>子组件:{{user.age}}</h3>
<button @click="change">修改数据</button>
</div>
</template>
<script>
var vm=new Vue({ //父组件
el:'#itany',
data:{
name:'tom',
user:{
name:'zhangsan',
age:24
}
},
components:{
'my-hello':{ //子组件
template:'#hello',
props:['name','user'],
data(){
return {
username:this.name //方式1:将数据存入另一个变量中再操作
}
},
methods:{
change(){
// this.username='alice';
// this.name='alice';
// this.$emit('update:name','alice'); //方式2:a.使用.sync,需要显式地触发一个更新事件
this.user.age=18;
}
}
}
}
});
</script>
</body>
</html>