简体   繁体   English

将 v-model 链接到 Vue.js 2 中的对象属性

[英]Link v-model to a object's property in Vue.js 2

I use Vue.js 2 and I have this array, obtained from this API call ( https://developers.themoviedb.org/3/genres/get-movie-list this one) that I've used to make a select in HTML: I use Vue.js 2 and I have this array, obtained from this API call ( https://developers.themoviedb.org/3/genres/get-movie-list this one) that I've used to make a select in HTML:

 "genres": [
        {
            "id": 28,
            "name": "Azione"
        },
        {
            "id": 12,
            "name": "Avventura"
        },
        {
            "id": 16,
            "name": "Animazione"
        },
        {
            "id": 35,
            "name": "Commedia"
        },
        {
            "id": 80,
            "name": "Crime"
        },
        {
            "id": 99,
            "name": "Documentario"
        },
        {
            "id": 18,
            "name": "Dramma"
        },
        {
            "id": 10751,
            "name": "Famiglia"
        },
        {
            "id": 14,
            "name": "Fantasy"
        },
        {
            "id": 36,
            "name": "Storia"
        },
        {
            "id": 27,
            "name": "Horror"
        },
        {
            "id": 10402,
            "name": "Musica"
        },
        {
            "id": 9648,
            "name": "Mistero"
        },
        {
            "id": 10749,
            "name": "Romance"
        },
        {
            "id": 878,
            "name": "Fantascienza"
        },
        {
            "id": 10770,
            "name": "televisione film"
        },
        {
            "id": 53,
            "name": "Thriller"
        },
        {
            "id": 10752,
            "name": "Guerra"
        },
        {
            "id": 37,
            "name": "Western"
        }
    ]
}

What I want is to link the v-model , which is declared in Js as an empty string, to the property id of this array.我想要的是将在 Js 中声明为空字符串的v-model链接到该数组的属性id I can't extract the property and use an array with only it, because options' name is a v-for that prints genres.name At the point I am now when I do the console.log on the v-model it returns an empty string, means that v-model doesn't take any value.我无法提取属性并仅使用它的数组,因为选项的名称是一个v-for打印genres.name现在当我在v-model上执行console.log时它返回一个空字符串,表示v-model不带任何值。 The select is this: select 是这样的:

        <label for="title">Title</label>
        <select id="title" name="title"
        @change="filterAlbums(selectedGenre)"
        v-model="selectedGenre">
            <option value="">All</option>
            <option v-for="genre in genresName" value="">{{genre.name}}</option>
        </select>

In data the v-model variable is declared this way:在 data 中,v-model 变量是这样声明的:

selectedGenre: '',

I check the v-model status here in this function我在此 function 中检查了v-model状态

filterAlbums(selectedGenre) {
   console.log(selectedGenre);
        }

Do you mean that this array is a list of elements to be options in a select?你的意思是这个数组是一个元素列表,是 select 中的选项吗? If yes, you need to bind every option value (previously generated in a loop by v-for) with the element id:如果是,则需要将每个选项值(先前由 v-for 在循环中生成)与元素 id 绑定:

<select v-model="someValueWithInitialEmptyString">
   <option v-for="element in genres" :key="element.id" :value="element.id">
      {{element.name}}
   </option>
</select>

How are you linking this array to that attribute?您如何将此数组链接到该属性? Where is this array coming from?这个数组是从哪里来的? You should show some more code.您应该显示更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM