2017-04-02 76 views
2

我有一個表單,目前正在使用jQuery來執行一系列ajax請求,然後根據數據動態更改選擇選項。我認爲這將是很好的重新實現vue.js作爲一種學習經驗。但似乎遇到了一個障礙,將動態選項添加到<select>的最佳方式是什麼。文檔中的這個鏈接(http://012.vuejs.org/guide/forms.html#Dynamic_Select_Options)看起來正是我所需要的,但是我在下面的代碼中複製了一些問題。任何想法我可能做錯了什麼?任何提示都表示讚賞!渲染動態選擇選項

<script src="https://unpkg.com/vue"></script> 

<script type="text/x-template" id="add-match-template"> 
    <select v-model="selected_game" options="games"></select> 
</script> 

<div id="add_match_form"> 
    <add-match-form></add-match-form> 
</div> 

<script type="text/javascript"> 
    Vue.component('add-match-form', { 
     template: '#add-match-template', 
     data: function() { 
      return { 
       games: [ 
        {'value': 1, 'text': 'Game 1'}, 
        {'value': 4, 'text': 'Game 4'} 
       ], 
       selected_game: null 
      } 
     } 
    }) 

    new Vue({ 
     el: "#add_match_form" 
    }) 
</script> 
+0

嘗試在選項前添加':',使其成爲':options =「games」'。我會發佈一個答案,如果它的工作,但看起來像首先要檢查。 – webnoob

+0

您是否使用Vue 2?您鏈接的文檔非常舊。 – Bert

+0

@BertEvans啊,我怎麼也不能抓住那個。我正在使用版本2. – jsm1th

回答

3

在Vue 2中,您不再以這種方式綁定選項。這裏是current documentation

Vue.component('add-match-form', { 
    template: '#add-match-template', 
    data: function() { 
    return { 
     games: [ 
     {'value': 1, 'text': 'Game 1'}, 
     {'value': 4, 'text': 'Game 4'} 
     ], 
     selected_game: null 
    } 
    } 
}) 

new Vue({ 
    el: "#add_match_form" 
}) 

模板:

<div id="add_match_form"> 
    <add-match-form></add-match-form> 
</div> 

<template id="add-match-template"> 
    <select v-model="selected_game"> 
    <option v-for="game in games" value="game.value">{{game.text}}</option> 
    </select> 
</template> 

Example

+0

好點,甚至沒有注意到文檔是舊:) – webnoob

+0

我不相信我沒有意識到我在舊文檔。我也嘗試過這樣的事情,並且遇到了問題,因爲我正在嘗試在遊戲「value =」value「> {{text}}'中做這樣的'

+0

@ jsm1th我經常遇到doc問題,所以我現在檢查:) – Bert