[英]v-model for child component and v-model inside child component Vue
Is there a way to simplify this code?有没有办法简化这段代码?
The button should also change the localValue of the child.该按钮还应更改子项的 localValue。
Vue.component('my-input', { template: ` <div> <b>My Input:</b> <br> localValue: {{ localValue }} <br> <input v-model="localValue"> </div> `, props: ['value'], data() { return { localValue: this.value } }, watch: { value () { this.localValue = this.value }, localValue () { this.$emit('input', this.localValue) } } }) new Vue({ el: '#app', data: () => ({ parentValue: 'Inital value' }), methods: { change () { this.parentValue = 'Changed value' } } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script> <div id="app"> <my-input v-model="parentValue"></my-input> <button @click="change">Change</button><br> parentValue: {{ parentValue }} </div>
I have always faced difficulties when I need to do so.当我需要这样做时,我总是会遇到困难。
I will be very grateful for the help!我将非常感谢您的帮助!
If you avoid using v-model
inside your custom form component, you really only need如果你避免在你的自定义表单组件中使用v-model
,你真的只需要
<b>My Input:</b> <br>
localValue: {{ value }} <br>
<input :value="value" @input="$emit('input', $event.target.value)">
No data
, no watch
, that's it.没有data
,没有watch
,就是这样。
See https://v2.vuejs.org/v2/guide/components.html#Using-v-model-on-Components见https://v2.vuejs.org/v2/guide/components.html#Using-v-model-on-Components
If you really want something representing a value local to your component, the Vue docs favour using computed values over watchers (ref: https://v2.vuejs.org/v2/guide/computed.html#Watchers ).如果您真的想要代表组件本地值的东西,Vue 文档倾向于使用计算值而不是观察者(参考: https ://v2.vuejs.org/v2/guide/computed.html#Watchers)。
The idea here is to create a computed value with getter and setter to facilitate a simplified one-way data flow.这里的想法是使用getter和setter创建一个计算值,以促进简化的单向数据流。
Vue.component('my-input', { template: `<div><b>My Input:</b> <br>localValue: {{ localValue }} <br><input v-model="localValue"></div>`, props: ['value'], computed: { localValue: { get () { return this.value }, set (value) { this.$emit('input', value) } } } }) new Vue({ el: '#app', data: () => ({ parentValue: 'Inital value' }), methods: { change () { this.parentValue = 'Changed value' } } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script> <div id="app"> <my-input v-model="parentValue"></my-input> <button @click="change">Change</button><br> parentValue: {{ parentValue }} </div>
How to pass complex objects to child component (potentially down a few layers):如何将复杂对象传递给子组件(可能向下几层):
Parent component:父组件:
<child v-model='parentEntity' />
Child component:子组件:
model: {
prop: 'modelValue',
event: 'update:modelValue',
},
props: {
modelValue: {
type: Object,
required: true,
},
},
...
entity: {
// getter
get() {
return Object.assign({}, this.modelValue);
},
// setter
set(newValue) {
this.$emit('update:modelValue', newValue);
},
},
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.