2017-02-10 9 views
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:

答えて

1

あなたが持っている問題は、最終的なthis.options変数ではselect要素に入力してください、あなたは次のようにfetchList方法内のコード変更することができ、配列、ハッシュNATであります働くフィドルhereを見てください。

+0

お願いします。なぜsetTimeoutを使うのですか? setTimeoutなしでは、それは動作します –

+0

@mosestoh私はあなたの非同期呼び出しをシミュレートするために 'setTimeout'を使いました。 – Saurabh

+0

私を助けてくれてありがとう –

関連する問題