[英]How to bind function in vue js v-model?
I asked a question which might be unclear to someone.我问了一个可能有人不清楚的问题。 So, I deleted that question and ask again with new approach.
所以,我删除了那个问题,并用新的方法再次提问。 I have an API response something like this:
我有一个 API 响应是这样的:
{
id: 2,
name: 'Item 1',
items: [
{
slug: 'Phase 1',
values: [
{
highValue: '12',
lowValue: '8',
color: 'red'
},
{
highValue: '15',
lowValue: '5',
color: 'green'
}
]
},
{
slug: 'Phase 2',
values: [
{
highValue: '14',
lowValue: '6',
color: 'red'
},
{
highValue: '15',
lowValue: '5',
color: 'green'
}
]
}
]
},
{
id: 3,
name: 'Item 2',
items: [
{
slug: 'CBC',
values: [
{
highValue: '10',
lowValue: '7',
color: 'green'
},
{
highValue: '12',
lowValue: '3',
color: 'red'
}
]
}
]
}
I have static block for High Value
, Low Value
, Red
& Green
in my HTML.我的 HTML 中有用于
High Value
、 Low Value
、 Red
和Green
的 static 块。 So, for those static blocks, I need to pick appropriate value from the response.因此,对于那些 static 块,我需要从响应中选择适当的值。 For example, for
High Value
& Red
block, I need to pick highValue
from the response when color: 'red'
.例如,对于
High Value
& Red
块,我需要在color: 'red'
时从响应中选择highValue
。 So, I write four function for example:所以,我写了四个 function 例如:
redHigh (item) {
const res = item.filter(obj => {
return obj.color === 'red'
})
return res[0].highValue
}
Now, I tried to bind the function in v-model
like this way:现在,我尝试像这样在
v-model
中绑定 function:
<v-text-field
outlined
:v-model="redHigh(sub.values)"
placeholder="High"
></v-text-field>
But, that was not working.但是,那行不通。 If I wrote
:value
instead of :v-model
, that would work.如果我写的是
:value
而不是:v-model
,那会起作用。 But, in that case I don't get the changed value after clicking save
button.但是,在这种情况下,单击
save
按钮后我没有得到更改的值。
save (formIndex) {
console.log(this.items[formIndex])
}
How to solve that?如何解决?
v-model is not for a function. v-model 不适用于 function。
it's for a Vue's data property它用于 Vue 的数据属性
however, I understand your app requirement.但是,我了解您的应用程序要求。
you just need to create Vue computed properties, that can generate a dynamic array using a custom function您只需要创建 Vue 计算属性,即可使用自定义 function 生成动态数组
and the last thing is you have to bind input event from your text field最后一件事是你必须从你的文本字段绑定输入事件
you can read $emit
or v-bind
documentation about it您可以阅读有关它的
$emit
或v-bind
文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.