回答編集履歴
3
微修正
answer
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
###### watchの場合
|
1
|
+
###### watchの場合(追記)
|
2
2
|
|
3
|
-
**[DEMO - JSFiddle](https://jsfiddle.net/eywraw8t/
|
3
|
+
**[DEMO - JSFiddle](https://jsfiddle.net/eywraw8t/444403/)**
|
4
4
|
|
5
5
|
```html
|
6
6
|
<style>
|
2
追記しました
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
###### watchの場合
|
2
2
|
|
3
3
|
**[DEMO - JSFiddle](https://jsfiddle.net/eywraw8t/444296/)**
|
4
4
|
|
@@ -14,6 +14,69 @@
|
|
14
14
|
.childcomp {
|
15
15
|
background: gray;
|
16
16
|
width: 100px;
|
17
|
+
height: 100px;
|
18
|
+
}
|
19
|
+
</style>
|
20
|
+
<body>
|
21
|
+
<div id="app">
|
22
|
+
<button v-on:click="increment">increment</button>
|
23
|
+
<childcomp ref="child" v-bind:count="state.count"></childcomp>
|
24
|
+
<textarea>{{ state.log }}</textarea>
|
25
|
+
</div>
|
26
|
+
<script>
|
27
|
+
let state = { log: '', count: 0 };
|
28
|
+
|
29
|
+
Vue.component('childcomp', {
|
30
|
+
props: ['count'],
|
31
|
+
template: `
|
32
|
+
<div class="childcomp">
|
33
|
+
<button v-on:click="increment">increment</button>
|
34
|
+
<div>{{ count }}</div>
|
35
|
+
</div>
|
36
|
+
`,
|
37
|
+
methods: {
|
38
|
+
increment: function() {
|
39
|
+
state.count += 1;
|
40
|
+
},
|
41
|
+
},
|
42
|
+
watch: {
|
43
|
+
count: function() {
|
44
|
+
state.log += 'on counted!\n';
|
45
|
+
}
|
46
|
+
}
|
47
|
+
});
|
48
|
+
|
49
|
+
vm = new Vue({
|
50
|
+
el: '#app',
|
51
|
+
data: { state },
|
52
|
+
methods: {
|
53
|
+
increment: function(event) {
|
54
|
+
state.count += 1;
|
55
|
+
},
|
56
|
+
},
|
57
|
+
});
|
58
|
+
</script>
|
59
|
+
</body>
|
60
|
+
```
|
61
|
+
|
62
|
+
###### $ref['child']の場合
|
63
|
+
|
64
|
+
試してたら、`this.$refs['child'].someMethod();` でいけました。ただ、試している中で、`this.$refs['child'][0].someMethod();` でいけたこともあって、すこし謎です。
|
65
|
+
|
66
|
+
**[DEMO - JSFiddle](https://jsfiddle.net/boilerplate/vue)**
|
67
|
+
|
68
|
+
```html
|
69
|
+
<style>
|
70
|
+
#app {
|
71
|
+
background: salmon;
|
72
|
+
width: 200px;
|
73
|
+
}
|
74
|
+
textarea {
|
75
|
+
height: 300px;
|
76
|
+
}
|
77
|
+
.childcomp {
|
78
|
+
background: gray;
|
79
|
+
width: 100px;
|
17
80
|
height: 50px;
|
18
81
|
}
|
19
82
|
</style>
|
@@ -57,7 +120,4 @@
|
|
57
120
|
});
|
58
121
|
</script>
|
59
122
|
</body>
|
60
|
-
```
|
123
|
+
```
|
61
|
-
|
62
|
-
あと、functionのスペルがミスってます。
|
63
|
-
また、コンポーネントを利用するときって、import とか compontents: { child-component } とかやったようなやらなかったような…。
|
1
追記しましあ
answer
CHANGED
@@ -57,4 +57,7 @@
|
|
57
57
|
});
|
58
58
|
</script>
|
59
59
|
</body>
|
60
|
-
```
|
60
|
+
```
|
61
|
+
|
62
|
+
あと、functionのスペルがミスってます。
|
63
|
+
また、コンポーネントを利用するときって、import とか compontents: { child-component } とかやったようなやらなかったような…。
|