質問編集履歴

1

henkou

2019/01/15 04:01

投稿

tokuwgawa
tokuwgawa

スコア45

test CHANGED
File without changes
test CHANGED
@@ -71,3 +71,203 @@
71
71
  `template`の中の`layers`の中で変更が起きてもこの書き方の`computed`では変更が起きません。
72
72
 
73
73
  どうすればオブジェクトの中の値変更まで`computed`で検知してくれる様な処理にできますか?
74
+
75
+
76
+
77
+ #追記
78
+
79
+ 自分の実装載せます。
80
+
81
+ ```
82
+
83
+ //親コンポーネント
84
+
85
+ <template>
86
+
87
+ <div class="create">
88
+
89
+ <Preview :tmp_template="template"/>
90
+
91
+ <image :tmp_template="template" v-model="template" @setTmpTemplate="setTemplate"/>
92
+
93
+ </div>
94
+
95
+ </template>
96
+
97
+
98
+
99
+ <script>
100
+
101
+ export default {
102
+
103
+ components: {
104
+
105
+ Preview: () => import('@/components/preview.vue'),
106
+
107
+ Image: () => import('@/components/image.vue'),
108
+
109
+ },
110
+
111
+ data() {
112
+
113
+ return {
114
+
115
+ template: {
116
+
117
+ id: "",
118
+
119
+ width: 0,
120
+
121
+ height: 0,
122
+
123
+ layers: [],
124
+
125
+ },
126
+
127
+ }
128
+
129
+ },
130
+
131
+ methods: {
132
+
133
+ setTemplate(template) {
134
+
135
+ this.template = template
136
+
137
+ },
138
+
139
+ },
140
+
141
+ }
142
+
143
+ </script>
144
+
145
+
146
+
147
+ ```
148
+
149
+ 親コンポーネントで`computed`で検知するデータを作成しています。
150
+
151
+ ```
152
+
153
+ //子コンポーネント
154
+
155
+ //Image.vue
156
+
157
+ <template>
158
+
159
+ <div>
160
+
161
+ <div v-for="image in layer">
162
+
163
+ <el-input v-model="image.value"></el-input>
164
+
165
+ </div>
166
+
167
+ </div>
168
+
169
+ </template>
170
+
171
+
172
+
173
+ <script>
174
+
175
+ export default {
176
+
177
+ props: ['tmp_template'],
178
+
179
+ computed: {
180
+
181
+ layer() {
182
+
183
+ return this.tmp_template.layers.filter(function (layer) {
184
+
185
+ if(layer.type === 'image') {
186
+
187
+ console.log(layer)
188
+
189
+ return layer
190
+
191
+ }
192
+
193
+ })
194
+
195
+ }
196
+
197
+ },
198
+
199
+ }
200
+
201
+ </script>
202
+
203
+ ```
204
+
205
+ `image.vue`で`Object`の一番下のネストの値を変更しています。
206
+
207
+
208
+
209
+ ```
210
+
211
+ //子コンポーネント
212
+
213
+ //preview.vue
214
+
215
+ <script>
216
+
217
+ export default {
218
+
219
+ props: ['tmp_template'],
220
+
221
+ mounted() {
222
+
223
+ this.ctx = this.$el.firstChild.firstElementChild.getContext('2d')
224
+
225
+ this.drawCreative()
226
+
227
+ },
228
+
229
+ computed: {
230
+
231
+ template() {
232
+
233
+ return this.tmp_template
234
+
235
+ }
236
+
237
+ },
238
+
239
+ watch: {
240
+
241
+ template(val) {
242
+
243
+ this.drawCreative()
244
+
245
+ }
246
+
247
+ },
248
+
249
+ methods: {
250
+
251
+ drawCreative() {
252
+
253
+ this.ctx.canvas.width = this.tmp_template.width
254
+
255
+ this.ctx.canvas.height = this.tmp_template.height
256
+
257
+ this.tmp_template.layers.forEach(layer => {
258
+
259
+ this.ctx.fillText(layer.value, layer.x_position, layer.y_position - -10)
260
+
261
+ });
262
+
263
+ },
264
+
265
+ },
266
+
267
+ }
268
+
269
+ </script>
270
+
271
+ ```
272
+
273
+ 別の子コンポーネントで`image.vue`で入力して値変更された`tmp_template`を監視しているのですが、検知されません。