回答編集履歴

3

微修正

2018/11/02 08:36

投稿

退会済みユーザー
test CHANGED
@@ -1,8 +1,8 @@
1
- ###### watchの場合
1
+ ###### watchの場合(追記)
2
-
3
-
4
-
2
+
3
+
4
+
5
- **[DEMO - JSFiddle](https://jsfiddle.net/eywraw8t/444296/)**
5
+ **[DEMO - JSFiddle](https://jsfiddle.net/eywraw8t/444403/)**
6
6
 
7
7
 
8
8
 

2

追記しました

2018/11/02 08:36

投稿

退会済みユーザー
test CHANGED
@@ -1,8 +1,134 @@
1
+ ###### watchの場合
2
+
3
+
4
+
5
+ **[DEMO - JSFiddle](https://jsfiddle.net/eywraw8t/444296/)**
6
+
7
+
8
+
9
+ ```html
10
+
11
+ <style>
12
+
13
+ #app {
14
+
15
+ background: salmon;
16
+
17
+ width: 200px;
18
+
19
+ }
20
+
21
+ textarea {
22
+
23
+ height: 300px;
24
+
25
+ }
26
+
27
+ .childcomp {
28
+
29
+ background: gray;
30
+
31
+ width: 100px;
32
+
33
+ height: 100px;
34
+
35
+ }
36
+
37
+ </style>
38
+
39
+ <body>
40
+
41
+ <div id="app">
42
+
43
+ <button v-on:click="increment">increment</button>
44
+
45
+ <childcomp ref="child" v-bind:count="state.count"></childcomp>
46
+
47
+ <textarea>{{ state.log }}</textarea>
48
+
49
+ </div>
50
+
51
+ <script>
52
+
53
+ let state = { log: '', count: 0 };
54
+
55
+
56
+
57
+ Vue.component('childcomp', {
58
+
59
+ props: ['count'],
60
+
61
+ template: `
62
+
63
+ <div class="childcomp">
64
+
65
+ <button v-on:click="increment">increment</button>
66
+
67
+ <div>{{ count }}</div>
68
+
69
+ </div>
70
+
71
+ `,
72
+
73
+ methods: {
74
+
75
+ increment: function() {
76
+
77
+ state.count += 1;
78
+
79
+ },
80
+
81
+ },
82
+
83
+ watch: {
84
+
85
+ count: function() {
86
+
87
+ state.log += 'on counted!\n';
88
+
89
+ }
90
+
91
+ }
92
+
93
+ });
94
+
95
+
96
+
97
+ vm = new Vue({
98
+
99
+ el: '#app',
100
+
101
+ data: { state },
102
+
103
+ methods: {
104
+
105
+ increment: function(event) {
106
+
107
+ state.count += 1;
108
+
109
+ },
110
+
111
+ },
112
+
113
+ });
114
+
115
+ </script>
116
+
117
+ </body>
118
+
119
+ ```
120
+
121
+
122
+
123
+ ###### $ref['child']の場合
124
+
125
+
126
+
1
127
  試してたら、`this.$refs['child'].someMethod();` でいけました。ただ、試している中で、`this.$refs['child'][0].someMethod();` でいけたこともあって、すこし謎です。
2
128
 
3
129
 
4
130
 
5
- **[DEMO - JSFiddle](https://jsfiddle.net/eywraw8t/444296/)**
131
+ **[DEMO - JSFiddle](https://jsfiddle.net/boilerplate/vue)**
6
132
 
7
133
 
8
134
 
@@ -116,10 +242,4 @@
116
242
 
117
243
  </body>
118
244
 
119
- ```
245
+ ```
120
-
121
-
122
-
123
- あと、functionのスペルがミスってます。
124
-
125
- また、コンポーネントを利用するときって、import とか compontents: { child-component } とかやったようなやらなかったような…。

1

追記しましあ

2018/11/02 08:35

投稿

退会済みユーザー
test CHANGED
@@ -116,4 +116,10 @@
116
116
 
117
117
  </body>
118
118
 
119
- ```
119
+ ```
120
+
121
+
122
+
123
+ あと、functionのスペルがミスってます。
124
+
125
+ また、コンポーネントを利用するときって、import とか compontents: { child-component } とかやったようなやらなかったような…。