在我的管理应用程序中,我有一个,在这个组件中,我还有Vue 2的羽毛笔丰富的编辑器,它使用v-model的数据,现在我想把v-model从子vue-2-editor传递到我自己的父组件,文档说你可以在你的组件中有自定义的v-model,并使用该值发出一个'input‘事件,但是我如何才能将一个v-model传递给另一个,从子组件到父组件。
我正在使用vue 2编辑器,一个使用Vue.js和Quill的文本编辑器:https://github.com/davidroyer/vue2-editor
我的组件:
<template>
<div style="width:auto; height:auto; display:flex; flex-direction.column;">
<button @click="editorVisible = true">Show Editor</button>
<vue-editor v-model="value" :editorToolbar="customToolbar" useCustomImageHandler @imageAdded="handleImageAdded"></vue-editor>
</div>
</template>
<!--SCRIPTS-->
<script>
import { VueEditor } from 'vue2-editor';
export default {
name: 'ADMINeditor',
props:
{
value:{ required:true, type:String }
},
data()
{
return {
editorVisible:false
}
},
methods:
{
wrote()
{
this.$emit('input', this.value);
}
}
}
</script>
<!--STYLES-->
<style scoped>
</style>我希望能够做到:
<admin-editor v-model="text"></admin-editor>有关自定义组件中的-model的更多信息。
发布于 2019-11-16 13:49:16
TL;DR
<vue-editor :value="value" @input="$emit('input' $event)" />正如您所说的,要在组件中支持v-model,您需要添加一个模型属性并发出一个模型事件,以便让父级知道您想要更新数据。
默认情况下,v-model使用一个value属性和一个input事件,但是由于是2.2.0+,所以您可以使用customize the component v-model。
<vue-editor>组件使用v-model默认属性和事件,因此它需要一个value属性,并在数据更新时发出input事件。
所以:
<vue-editor v-model="value" />
等同于:
<vue-editor :value="xxx" @input="onXxxUpdate"
您的<admin-editor>组件还需要支持v-model,因此您需要执行与<vue-editor>组件相同的操作,添加模型属性并发出模型事件。
然后,只要<admin-editor>组件发出一个input事件,就从<vue-editor>发出一个input事件。
<template>
<vue-editor :value="value" @input="onEditorUpdate" />
</template>
<script>
import { VueEditor } from 'vue2-editor'
export default {
name: 'AdminEditor',
props: {
value: {
type: String,
default: '',
},
},
methods: {
onEditorUpdate(newVal) {
// ^^^^^^
// <vue-editor> input event payload
this.$emit('input', newVal)
},
},
}
</script>发布于 2019-11-16 08:06:30
您可以通过创建单独的私有变量(在data()中,而不是属性)来实现这一点,并将其用作vue-editor组件上的v-model,然后查看它并在更改时发出@input事件,还需要从父v-model接收更新,您需要查看value属性并更新私有变量。
<template>
<div style="width:auto; height:auto; display:flex; flex-direction.column;">
<button @click="editorVisible = true">Show Editor</button>
<vue-editor v-model="pvalue" :editorToolbar="customToolbar" useCustomImageHandler @imageAdded="handleImageAdded"></vue-editor>
</div>
</template>
<!--SCRIPTS-->
<script>
import { VueEditor } from 'vue2-editor';
export default {
props:
{
value:{ required:true, type:String }
},
data()
{
return {
pvalue: '',
}
},
watch: {
value(val){
this.pvalue = val;
},
pvalue(val){
this.$emit('input', val);
}
}
}
</script>注意:v-model="pvalue"
https://stackoverflow.com/questions/58886181
复制相似问题