【问题标题】:vue, how dynamically, programically, on click add component to the DOM specific place?vue,如何以编程方式动态地单击将组件添加到 DOM 特定位置?
【发布时间】: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 &lt;b&gt;bold text&lt;/b&gt;, and inserted image from this component &lt;vue-simple-image :src='images/my_image_name.png' /&gt;, another text and html tags." 但我只能从contenteditable 获取此生成的html:"Some text, some &lt;b&gt;bold text&lt;/b&gt;, and inserted image from this component &lt;img src='images/my_image_name.png' /&gt;, another text and html tags."。我该怎么做?

标签: html vue.js dynamic tags


【解决方案1】:

借助 https://stackoverflow.com/a/2925633/7741865 和动态创建子组件,您应该可以实现您想要的。示例:

addContainer() {
  // dynamically create component, replace 'Child' with your component
  var ComponentClass = Vue.extend(Child);
  var instance = new ComponentClass();
  instance.$mount();

  // get the caret position and insert component at that place
  var sel, range;
  if (window.getSelection) {
    sel = window.getSelection();
    if (sel.getRangeAt && sel.rangeCount) {
      range = sel.getRangeAt(0);
      range.deleteContents();
      range.insertNode(instance.$el);
      // remove the highlight (if you want)
      window.getSelection().removeAllRanges();
    }
  }
}

SANDBOX

【讨论】:

  • 我想将带有点击事件的图像组件插入到 contenteditable div 中。如何使用该组件的 v-model 仅生成 标签?如何恢复该组件并从数据库中再次插入它?
  • 我投票赞成这个答案。看起来这就是我正在搜索的内容,但对如何在我的组件中使用它有一些疑问。
  • @webprogrammer,如果您只想插入一张图片,请将Child 模板更改为只有一张图片,并将网址传递给孩子。这应该让您开始:codesandbox.io/s/vue-template-m1ypk 您对数据库的意思尚不清楚。如果该沙盒没有帮助,请考虑提出一个新问题 :)
  • 好的,我会问新问题。感谢您提供带有图片的新代码框。
  • 我在这里 stackoverflow.com/questions/59849554/… 为我的问题创建了扩展问题。你有时间可以看看吗?谢谢!
猜你喜欢
  • 2013-11-28
  • 2021-05-16
  • 2011-10-31
  • 1970-01-01
  • 2022-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多