質問するログイン新規登録

質問編集履歴

1

henkou

2019/01/15 04:01

投稿

tokuwgawa
tokuwgawa

スコア45

title CHANGED
File without changes
body CHANGED
@@ -34,4 +34,104 @@
34
34
  }
35
35
  ```
36
36
  `template`の中の`layers`の中で変更が起きてもこの書き方の`computed`では変更が起きません。
37
- どうすればオブジェクトの中の値変更まで`computed`で検知してくれる様な処理にできますか?
37
+ どうすればオブジェクトの中の値変更まで`computed`で検知してくれる様な処理にできますか?
38
+
39
+ #追記
40
+ 自分の実装載せます。
41
+ ```
42
+ //親コンポーネント
43
+ <template>
44
+ <div class="create">
45
+ <Preview :tmp_template="template"/>
46
+ <image :tmp_template="template" v-model="template" @setTmpTemplate="setTemplate"/>
47
+ </div>
48
+ </template>
49
+
50
+ <script>
51
+ export default {
52
+ components: {
53
+ Preview: () => import('@/components/preview.vue'),
54
+ Image: () => import('@/components/image.vue'),
55
+ },
56
+ data() {
57
+ return {
58
+ template: {
59
+ id: "",
60
+ width: 0,
61
+ height: 0,
62
+ layers: [],
63
+ },
64
+ }
65
+ },
66
+ methods: {
67
+ setTemplate(template) {
68
+ this.template = template
69
+ },
70
+ },
71
+ }
72
+ </script>
73
+
74
+ ```
75
+ 親コンポーネントで`computed`で検知するデータを作成しています。
76
+ ```
77
+ //子コンポーネント
78
+ //Image.vue
79
+ <template>
80
+ <div>
81
+ <div v-for="image in layer">
82
+ <el-input v-model="image.value"></el-input>
83
+ </div>
84
+ </div>
85
+ </template>
86
+
87
+ <script>
88
+ export default {
89
+ props: ['tmp_template'],
90
+ computed: {
91
+ layer() {
92
+ return this.tmp_template.layers.filter(function (layer) {
93
+ if(layer.type === 'image') {
94
+ console.log(layer)
95
+ return layer
96
+ }
97
+ })
98
+ }
99
+ },
100
+ }
101
+ </script>
102
+ ```
103
+ `image.vue`で`Object`の一番下のネストの値を変更しています。
104
+
105
+ ```
106
+ //子コンポーネント
107
+ //preview.vue
108
+ <script>
109
+ export default {
110
+ props: ['tmp_template'],
111
+ mounted() {
112
+ this.ctx = this.$el.firstChild.firstElementChild.getContext('2d')
113
+ this.drawCreative()
114
+ },
115
+ computed: {
116
+ template() {
117
+ return this.tmp_template
118
+ }
119
+ },
120
+ watch: {
121
+ template(val) {
122
+ this.drawCreative()
123
+ }
124
+ },
125
+ methods: {
126
+ drawCreative() {
127
+ this.ctx.canvas.width = this.tmp_template.width
128
+ this.ctx.canvas.height = this.tmp_template.height
129
+ this.tmp_template.layers.forEach(layer => {
130
+ this.ctx.fillText(layer.value, layer.x_position, layer.y_position - -10)
131
+ });
132
+ },
133
+ },
134
+ }
135
+ </script>
136
+ ```
137
+ 別の子コンポーネントで`image.vue`で入力して値変更された`tmp_template`を監視しているのですが、検知されません。