【发布时间】:2020-03-05 01:52:25
【问题描述】:
我有以下组件:
//wrapper component
<template>
<div class="form-group form-group-text">
<label v-if="label" :for="fieldId()">{{ label }}</label>
<tiny-wrapper :key="pk"
class="form-control builderEditor"
:id="id"
:name="fieldName()"
v-model="form[field.key]"
:init="editorSettings"
:content="field.content"
></tiny-wrapper>
</div>
</template>
<script>
import Vue from 'vue'
import BuilderHelper from './builder-helper'
import TinyWrapper from '../tiny-wrapper'
export default Vue.extend({
props: [
'pk',
'title',
'fieldKey',
'field',
'databaseName',
'required',
'disabled',
'options',
'label',
'locale',
'hidden',
],
mixins: [BuilderHelper],
components: {
'tiny-wrapper': TinyWrapper,
},
computed: {
editorSettings() {
return {
// editor_selector: '.builderEditor',
selector: '#' + this.id,
menubar: '',
toolbar: 'bold italic | link',
height: 150,
contextmenu: 'bold italic | link',
forced_root_block: false,
invalid_elements: 'script',
statusbar: false,
resize: false,
browser_spellcheck: true,
}
}
},
})
</script>
和
// child component
<template>
<textarea :id="id" ref="editor" class="form-control" :class="classList" :value="content"></textarea>
</template>
<script>
import Vue from 'vue'
import 'tinymce/tinymce'
export default Vue.extend({
props: {
init: {
type: Object,
},
id: {
type: String,
required: true,
},
classList: {
type: String,
},
value: {
type: String,
}
},
data: function () {
return {
content: '',
tinyOptions: {},
}
},
mounted() {
// this.content = this.value
this.tinyOptions = Object.assign(window.tinyMCESettings, {
selector: '#' + this.id,
init_instance_callback: this.initInstanceCallback,
}, this.init)
tinymce.init(this.tinyOptions)
},
methods: {
initInstanceCallback(editor) {
editor.setContent(this.value)
editor.on('change', e => {
this.update(editor)
})
editor.on('keyup', e => {
this.update(editor)
})
this.$parent.$on('reset', () => editor.setContent(''))
},
update(editor) {
this.content = editor.getContent()
this.$emit('input', this.content)
},
}
})
</script>
我在文档中使用了 10 次 wrapper-component。
子组件中的数据对于每个包装组件都是相同的,状态由最后实例化的子组件中的数据共享/覆盖。我做错了什么?
【问题讨论】:
-
不完全清楚你所说的'状态是共享/覆盖'是什么意思。您能否更明确地说明哪些位似乎是共享的,以及哪些观察导致您得出该结论?从代码中跳出来的第一件事是从
this.tinyOptions = Object.assign开始的位。这将在所有实例之间共享相同的tinyOptions对象。它们都将是对象window.tinyMCESettings,尽管该对象通过对Object.assign的调用不断更新。 -
this.tinyOptions应该是每个组件的本地,不是吗?考虑到它在data-property 中?在 tinyOptions 中有一个名为“selector”的属性,tinymce.init 使用它来确定哪个组件应该是一个小编辑器。早些时候我直接在数据属性上做了 Object.assigning,但同样的事情发生了。
标签: vuejs2 vue-component