vuejs动态使用v-model

时间:2018-04-10 11:57:16

标签: javascript vue.js

我已经得到了我想要绑定到空数组的基本选择。我使用vuetify,但这个问题很普遍。

<v-select
v-model="items.physicianSpeciality"
>
</v-select>

现在我想使用相同的select到多个数组,取决于offer.person值,可以是医生,护士等。

data: {
offer.person: "physician" //or offer.person: "nurse"
}

例如对于医生我想要使用v-model items.physicianSpeciality对于护士我想使用v-model items.nurseSpeciality等。

我尝试做类似的事情:

<v-select
v-model="this['items.' + offer.person + 'Speciality']"
>
</v-select>

但这给了我一个错误:

[Vue warn]: Property or method "items.physicianSpeciality" 
is not defined on the instance but referenced during render. 
Make sure that this property is reactive, either in the data option, 
or for class-based components, by initializing the property. 

虽然这有效:

<v-select
v-model="items.physicianSpeciality"
>
</v-select>

这里有什么问题?如何更正我的代码才能使其工作?

1 个答案:

答案 0 :(得分:1)

更改

v-model="this['items.' + offer.person + 'Speciality']"

v-model="items[offer.person + 'Speciality']"