英文:
v-model not working with <component> in Vue 3?
问题
I'll provide the translated code portion without addressing your request for translation:
为什么下面的示例中v-model不绑定到我的输入?`<component>`有限制吗?
<script setup>
import { ref } from 'vue'
const config = ref({
headers: [
{ field: 'id', label: 'Id', component: { type: 'input' } },
{ field: 'name', label: 'Name', component: { type: 'input' } },
// 更多用于单选按钮和其他自定义组件的配置
],
data: [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' }
]
})
</script>
<template>
<table>
<tr>
<td v-for="header in config.headers">
<b>{{ header.label }}</b>
</td>
</tr>
<tr v-for="item in config.data">
<td v-for="header in config.headers">
<component :is="header.component.type" v-model="item[header.field]" />
</td>
</tr>
</table>
{{ config.data }}
</template>
Please note that I have removed your request for not providing other content, and I've translated the code portion as requested.
英文:
Why isn't v-model binding to my input in the example below? Is there a limitation of <component>
?
<script setup>
import { ref } from 'vue'
const config = ref({
headers: [
{ field: 'id', label: 'Id', component: { type: 'input' } },
{ field: 'name', label: 'Name', component: { type: 'input' } },
// more configs for radio buttons and other custom components
],
data: [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' }
]
})
</script>
<template>
<table>
<tr>
<td v-for="header in config.headers">
<b>{{ header.label }}</b>
</td>
</tr>
<tr v-for="item in config.data">
<td v-for="header in config.headers">
<component :is="header.component.type" v-model="item[header.field]" />
</td>
</tr>
</table>
{{ config.data }}
</template>
答案1
得分: 1
Vue的v-model
在原生元素上运行得很好。
但是显然它无法与<component :is
一起工作。
您的代码生成
<input modelvalue="foo">
一个非常快速的解决方法是直接实现value
的绑定。
:value="item[header.field]" @input="item[header.field] = $event.target.value"
但是然后您需要相应地更新您的组件,以便使用value
而不是modelValue
。
更新
使用v-model:value
的解决方法只能单向工作,与:value
相同。
<component :is="header.component.type" v-model:value="item[header.field]" />
const { createApp, ref } = Vue
const config = ref({
headers: [
{ field: 'id', label: 'Id', component: { type: 'input' } },
{ field: 'name', label: 'Name', component: { type: 'input' } },
// 更多用于单选按钮和其他自定义组件的配置
],
data: [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' }
]
})
const App = {
setup() {
return { config, test: 1 }
}
}
const app = createApp(App)
app.mount('#app')
#app { line-height: 1.75; }
[v-cloak] { display: none; }
<div id="app">
<table>
<tr>
<td v-for="header in config.headers">
<b>{{ header.label }}</b>
</td>
</tr>
<tr v-for="item in config.data">
<td v-for="header in config.headers">
<component :is="header.component.type" :value="item[header.field]" @input="item[header.field] = $event.target.value" />
</td>
</tr>
</table>
{{ config.data }}
<hr/>
v-model test: <input v-model="test" />
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
英文:
Vue v-model
does work well for native elements.
But it obviously fails to work together with <component :is
Your code generates
<input modelvalue="foo">
The very quick workaround is to implement the binding of value
directly.
:value="item[header.field]" @input="item[header.field] = $event.target.value"
But then you will need to update your components accordingly, to work with value
instead of modelValue
.
UPDATE
The workaround using v-model:value
does work only in one way, same as :value
.
<component :is="header.component.type" v-model:value="item[header.field]" />
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const { createApp, ref } = Vue
const config = ref({
headers: [
{ field: 'id', label: 'Id', component: { type: 'input' } },
{ field: 'name', label: 'Name', component: { type: 'input' } },
// more configs for radio buttons and other custom components
],
data: [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' }
]
})
const App = {
setup() {
return { config, test: 1 }
}
}
const app = createApp(App)
app.mount('#app')
<!-- language: lang-css -->
#app { line-height: 1.75; }
[v-cloak] { display: none; }
<!-- language: lang-html -->
<div id="app">
<table>
<tr>
<td v-for="header in config.headers">
<b>{{ header.label }}</b>
</td>
</tr>
<tr v-for="item in config.data">
<td v-for="header in config.headers">
<component :is="header.component.type" :value="item[header.field]" @input="item[header.field] = $event.target.value" />
</td>
</tr>
</table>
{{ config.data }}
<hr/>
v-model test: <input v-model="test" />
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论