1
私のコンポーネントvue.jsはこのようなものです:オプションの回答を表示するにはどうすればいいですか? (vue.js 2)
<script>
export default{
name: 'CategoryBsSelect',
template: '\
<select class="form-control" v-model="selected" required>\
<option v-for="option in options" v-bind:value="option.id" v-bind:disabled="option.disabled">{{ option.name }}</option>\
</select>',
//props: {list: {type: String, default: ''}},
mounted() {
this.fetchList();
},
data() {
return {
selected: '',
options: [{id: '', name: 'Pilih Kategori'}]
};
},
methods: {
fetchList: function() {
this.$http.post(window.BaseUrl+'/member/category/list').then(function (response) {
//this.$set('list', response.data);
console.log(JSON.stringify(response.body))
Object.keys(response.body).forEach(function (key) {
console.log(key)
console.log(response.body[key])
this.$set(this.options, key, response.body[key]);
}, this);
});
},
}
};
</script>
console.log(JSON.stringify(response.body))
の結果:
{"20":"Category 1","21":"Category 2","22":"Category 3"}
が、私は選択の値に応答を表示します。しかし実行されると、コンソールに次のようなエラーが表示されます。
TypeError: Cannot read property 'id' of undefined
私を助けることができる人はいますか?
Object.keys(resp).forEach((key) => {
console.log(key)
console.log(resp[key])
this.options.push({
id: key,
name: resp[key]
})
});
てきたA:
お願いします。なぜsetTimeoutを使うのですか? setTimeoutなしでは、それは動作します –
@mosestoh私はあなたの非同期呼び出しをシミュレートするために 'setTimeout'を使いました。 – Saurabh
私を助けてくれてありがとう –