hob*_*_be 4 components nested vue.js v-model
我有一个第三方输入组件(一个vue文本字段)。
出于验证的原因,我更喜欢将此组件包装在我自己的包装中。
我的TextField.vue
<template>
<v-text-field
:label="label"
v-model="text"
@input="onInput"
@blur="onBlur"
:error-messages="this.getErrors(this.validation, this.errors)"
></v-text-field>
</template>
<script>
import VTextField from "vuetify/es5/components/VTextField";
import {vuelidateErrorsMixin} from '~/plugins/common.js';
export default {
name: "TextField",
props: ['label', 'value', 'validation', 'errors'],
mixins: [vuelidateErrorsMixin], //add vuelidate
data: function() {
return {
'text': this.value
}
},
components: {
VTextField
},
methods : {
onInput: function(value) {
this.$emit('input', value);
this.validation.$touch();
},
onBlur: function() {
this.validation.$touch();
}
},
watch: {
value: {
immediate: true,
handler: function (newValue) {
this.text = newValue
}
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
在另一个组件中使用
<template>
...
<TextField v-model="personal.email" label="Email"
:validation="$v.personal.email" :errors="[]"/>
...
</template>
<script>
...imports etc.
export default { ...
data: function() {
return {
personal: {
email: '',
name: ''
}
}
},
components: [ TextField ]
}
</script>
Run Code Online (Sandbox Code Playgroud)
这很好用,但是我想知道是否有比重新复制整个v模型方法更清洁的方法。现在,我的数据在2个地方+所有额外的(不需要的)事件处理中被复制...
我只想将反应性数据直接从原始模板传递到v文本字段。我的TextField实际上根本不需要访问该数据-仅通知文本已更改(通过@ input,@ blur处理程序完成)。我不希望使用VUEX,因为这在处理输入/表单方面有其自身的问题...
更接近这个...
<template>
<v-text-field
:label="label"
v-model="value" //?? SAME AS 'Mine'
@input="onNotify"
@blur="onNotify"
:error-messages="this.getErrors(this.validation, this.errors)"
></v-text-field>
</template>
<script>
import VTextField from "vuetify/es5/components/VTextField";
import {vuelidateErrorsMixin} from '~/plugins/common.js';
export default {
name: "TextField",
props: ['label', 'validation', 'errors'], //NO VALUE HERE as cannot use props...
mixins: [vuelidateErrorsMixin], //add vuelidate
components: {
VTextField
},
methods : {
onNotify: function() {
this.validation.$touch();
}
},
}
</script>
Run Code Online (Sandbox Code Playgroud)
我找不到任何可以做到这一点的东西。
我使用props + v-model包装。
您需要将valueprop向下转发到包装的组件,然后将update事件转发回备份(有关更多详细信息,请参见https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components):
<template>
<wrapped-component
:value='value'
@input="update"
/>
</template>
<script>
import wrappedComponent from 'wrapped-component'
export default {
components: { 'wrapped-component': wrappedComponent },
props: ['value'],
methods: {
update(newValue) { this.$emit('input', newValue); }
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
别的地方:
<my-wrapping-component v-model='whatever'/>
Run Code Online (Sandbox Code Playgroud)
小智 1
我创建了一个 mixin 来简化组件的包装。
您可以在此处查看示例。
mixin 重用与“数据”相同的模式来传递值,并在外部更改期间使用“监视”来更新值。
export default {
data: function() {
return {
dataValue: this.value
}
},
props: {
value: String
},
watch: {
value: {
immediate: true,
handler: function(newValue) {
this.dataValue = newValue
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在包装组件上,您可以使用“attrs”和“listeners”将所有属性和侦听器传递给您的子组件并覆盖您想要的内容。
<template>
<div>
<v-text-field
v-bind="$attrs"
solo
@blur="onBlur"
v-model="dataValue"
v-on="$listeners" />
</div>
</template>
<script>
import mixin from '../mixins/ComponentWrapper.js'
export default {
name: 'my-v-text-field',
mixins: [mixin],
methods: {
onBlur() {
console.log('onBlur')
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1134 次 |
| 最近记录: |