Vue:v-model 不适用于动态组件
Vue: v-model doesn't work with dynamic components
例如:<component v-model='foo' :is='boo' ...>
。
foo
的值在输入过程中保持不变。
我已经尝试解决这个问题很长时间了。我检查了很多问题和线索,但其中 none 对我有帮助。
HTML 不起作用:
<component
:is="field.component"
:key="key"
:name="field.name"
v-for="(field, key) in integration_data"
v-model="field.value"
>
</component>
HTML 工作正常:
<input
:key="key"
:name="field.name"
v-for="(field, key) in integration_data"
v-model="field.value"
>
Vue 控制器:
export default {
init: function (init_data) {
return new Vue({
data: {
integration_data: [
{name: 'field_name0', component: 'input', value: ''},
{name: 'field_name0', component: 'input', value: ''},
]
},
});
}
}
v-model
与您可能尝试做的事情无关。看起来您正在尝试动态插入组件。这正是 :is
所做的。现在,要将数据传递给组件,您应该使用 props
.
例如:
你的 Vue 实例:
const vm = new Vue({
el: '#app',
data: {
exampleValue: 'This value will be passed to the example component'
}
})
注册组件:
Vue.component('example-component', {
// declare the props
props: ['value'],
template: '<span>{{ value}}</span>'
})
然后像这样使用它:
<example-component :value="exampleValue"></example-component>
或者:
<component :is="'example-component'" :value="exampleValue"></component>
您不能将 input
用作一种组件并期望它是本机输入元素。 :is
必须命名一个组件(如果需要,可以包含输入)。
那你要明白how v-model
works on components:
So for a component to work with v-model
, it should (these can be
configured in 2.2.0+):
- accept a
value
prop
- emit an
input
event with the new value
综合起来,您可以将 v-model
与 :is
结合使用。
new Vue({
el: '#app',
data: {
integration_data: [{
name: 'one',
component: 'one',
value: 'ok'
}]
},
components: {
one: {
props: ['name', 'value'],
template: '<div>{{name}} and <input v-model="proxyValue"><slot></slot></div>',
computed: {
proxyValue: {
get() { return this.value; },
set(newValue) { this.$emit('input', newValue); }
}
}
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<div id="app">
<component
:is="field.component"
v-for="(field, key) in integration_data"
:key="key"
:name="field.name"
v-model="field.value"
>
<div>{{field.value}}</div>
</component>
</div>
例如:<component v-model='foo' :is='boo' ...>
。
foo
的值在输入过程中保持不变。
我已经尝试解决这个问题很长时间了。我检查了很多问题和线索,但其中 none 对我有帮助。
HTML 不起作用:
<component
:is="field.component"
:key="key"
:name="field.name"
v-for="(field, key) in integration_data"
v-model="field.value"
>
</component>
HTML 工作正常:
<input
:key="key"
:name="field.name"
v-for="(field, key) in integration_data"
v-model="field.value"
>
Vue 控制器:
export default {
init: function (init_data) {
return new Vue({
data: {
integration_data: [
{name: 'field_name0', component: 'input', value: ''},
{name: 'field_name0', component: 'input', value: ''},
]
},
});
}
}
v-model
与您可能尝试做的事情无关。看起来您正在尝试动态插入组件。这正是 :is
所做的。现在,要将数据传递给组件,您应该使用 props
.
例如:
你的 Vue 实例:
const vm = new Vue({
el: '#app',
data: {
exampleValue: 'This value will be passed to the example component'
}
})
注册组件:
Vue.component('example-component', {
// declare the props
props: ['value'],
template: '<span>{{ value}}</span>'
})
然后像这样使用它:
<example-component :value="exampleValue"></example-component>
或者:
<component :is="'example-component'" :value="exampleValue"></component>
您不能将 input
用作一种组件并期望它是本机输入元素。 :is
必须命名一个组件(如果需要,可以包含输入)。
那你要明白how v-model
works on components:
So for a component to work with
v-model
, it should (these can be configured in 2.2.0+):
- accept a
value
prop- emit an
input
event with the new value
综合起来,您可以将 v-model
与 :is
结合使用。
new Vue({
el: '#app',
data: {
integration_data: [{
name: 'one',
component: 'one',
value: 'ok'
}]
},
components: {
one: {
props: ['name', 'value'],
template: '<div>{{name}} and <input v-model="proxyValue"><slot></slot></div>',
computed: {
proxyValue: {
get() { return this.value; },
set(newValue) { this.$emit('input', newValue); }
}
}
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<div id="app">
<component
:is="field.component"
v-for="(field, key) in integration_data"
:key="key"
:name="field.name"
v-model="field.value"
>
<div>{{field.value}}</div>
</component>
</div>