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

質問編集履歴

1

idをデータに追加し、propsにも定義しました。

2018/05/22 08:20

投稿

ebiebifry
ebiebifry

スコア8

title CHANGED
File without changes
body CHANGED
@@ -147,4 +147,84 @@
147
147
  });
148
148
  ```
149
149
  上記のthis.idに適当な数値を入力してみると動くのでidをいい感じに取得出来ればいいのだと思います。
150
- アドバイスのほどよろしくお願いいたします。
150
+ アドバイスのほどよろしくお願いいたします。
151
+
152
+ ### 追記
153
+ 追記・修正依頼の方にidという変数がdataにもpropsにも無いという指摘を受けたので下記のコードペンにて修正しました。
154
+ [https://codepen.io/ebifly/pen/RywWvg](https://codepen.io/ebifly/pen/RywWvg)
155
+ ```html
156
+ <div>
157
+ changeB
158
+ <name-list
159
+ :names="names"
160
+ <!--idのバインドを追加しました-->
161
+ :id="names.id"
162
+ @change-b="changeB"
163
+ ></name-list>
164
+ </div>
165
+ ```
166
+ ```js
167
+
168
+ Vue.component('name-list',{
169
+ template:'\
170
+ <select name="name-list" v-model="selected" @change="changeB">\
171
+ <option value="">該当なし</option>\
172
+ <option\
173
+ v-for="(name, index) in names"\
174
+ :value="name.title"\
175
+ >{{ name.title }}\
176
+ </option>\
177
+ </select>\
178
+ ',
179
+ //propsにidを追加しました
180
+ props: ['names', 'id'],
181
+ data: function data() {
182
+ return {
183
+ selected: '',
184
+ };
185
+ },
186
+ methods: {
187
+ changeB: function changeB(){
188
+ this.$emit('change-b', this.id)
189
+ }
190
+ }
191
+ });
192
+
193
+ var vm = new Vue({
194
+ el: '#main',
195
+ data: {
196
+ newNameText: '',
197
+ nextImg: 1,
198
+ nextId: 0,
199
+ names: []
200
+ },
201
+ methods: {
202
+ addNewName: function () {
203
+ if (this.newNameText !== '') {
204
+ if (this.nextImg > 10) {
205
+ this.nextImg = 1
206
+ }
207
+ this.names.push({
208
+ img: this.nextImg++,
209
+ //idのデータを追加しました。
210
+ id: this.nextId++,
211
+ title: this.newNameText,
212
+ status: 'a'
213
+ })
214
+ }
215
+ this.newNameText = ''
216
+ },
217
+ updateName: function updateName(id, name){
218
+ this.names[id].title = name
219
+ },
220
+ changeB: function changeB(id){
221
+ this.names[id].status = 'b'
222
+ }
223
+ }
224
+ })
225
+
226
+ vm.$mount('#main');
227
+ ```
228
+
229
+ この状態でも上手く動かないので、参照の仕方がおかしいのでしょうか?
230
+ アドバイスよろしくお願いしますm(_ _)m