【发布时间】:2020-04-06 20:38:06
【问题描述】:
我需要添加一个动态导入的组件,只需在 DOM 结构中的特定位置添加一个虚拟标签。不幸的是,我找到的每一种方法都没有解决我的问题。
我如何首先尝试:
父组件(Editor.vue):
<template>
<div>
<div class="toolbar">
<button @click="addContainer">ADD CONTAINER</button>
</div>
<div id="editor" ref="editor" contenteditable="true">
//here, when in conteneditable div is coursor I need to add dynamically, programically virtual tag <container />
</div>
</div>
</template>
和javascript
<script>
import container from '../container/Container.vue';
export default {
name: "editor",
components: {
container
},
data() {
return {};
},
methods: {
addContainer(){
document.execCommand('insertHTML', false, <container />); // execCommand let me add html in specyfic place, but I have error Unexpected token
}
},
};
子组件必须在用户需要的地方添加多少次用户需要(Container.vue)
<template>
<div
class="container editor--space"
@mouseover="highlightIn"
@mouseout="highlightOut"
contenteditable="true"
>
<div
class="editor--labelspace"
v-if="showLabel"
contenteditable="false"
>
container
</div>
{{ container }}
</div>
</template>
和javascript
<script>
export default {
name: "container",
data() {
return {
showLabel: false,
container: "Container here ..."
};
},
methods: {
highlightIn(){
this.showLabel = true;
},
highlightOut(){
this.showLabel = false;
}
}
};
</script>
也许有人可以给我一些想法,如何做到这一点?
【问题讨论】:
-
插入组件后,您的脚本如何?您是否将插入存储到数据库中?
-
我想将此代码存储到数据库中:
"Some text, some <b>bold text</b>, and inserted image from this component <vue-simple-image :src='images/my_image_name.png' />, another text and html tags."但我只能从contenteditable获取此生成的html:"Some text, some <b>bold text</b>, and inserted image from this component <img src='images/my_image_name.png' />, another text and html tags."。我该怎么做?