-
Notifications
You must be signed in to change notification settings - Fork 1
/
7. data vs props.html
60 lines (49 loc) · 1.67 KB
/
7. data vs props.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
60
<!DOCTYPE html>
<html>
<head>
<title>Data and models</title>
<script type="text/javascript" src="https://vuejs.org/js/vue.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>
<div id="my-app" class="container pt-4 d-flex flex-row">
<immutable inline-template :username="username">
<div class="card p-4 mr-4">
<h1 class="mb-2">Immutable prop</h1>
<button class="btn btn-danger" @click="changeName">Change name</button>
<code class="mt-2">{{ username }}</code>
</div>
</immutable>
<mutatable inline-template>
<div class="card p-4">
<h1 class="mb-2">mutatable data</h1>
<button class="btn btn-primary" @click="changeName">Change name</button>
<code class="mt-2">{{ username }}</code>
</div>
</mutatable>
</div>
<script type="text/javascript">
Vue.component('immutable', {
props: ['username'],
methods: { changeName() { this.username = 'new username'; } }
})
Vue.component('mutatable', {
data() {
return {
username: 'old username'
}
},
methods: { changeName() { this.username = 'new username'; } }
})
new Vue({
el: '#my-app',
data: {
username: 'old username'
},
mounted() {
setInterval(() => this.$forceUpdate(), 1000)
}
})
</script>
</body>
</html>