如果把vue组件动态添加到body上?

tools.js:

import Vue from 'vue'
 
/**
 * @param Component 组件实例的选项对象
 * @param props 组件实例中的prop
 */
export function create(Component, props) {
  const comp = new (Vue.extend(Component))({ propsData: props }).$mount()
   
  document.body.appendChild(comp.$el)
 
  comp.remove = () => {
    document.body.removeChild(comp.$el)
 
    comp.$destroy()
  }
 
  return comp
}

App.js:

<template>
  <div class="m-feedback-out-wrap">
    <el-button @click="handleAdd">添加</el-button>
  </div>
</template>

<script>
import { Feedback } from './feedback'
import { Button } from 'element-ui'
import { create } from './tools'
import './index.css'

let feeback
export default {
  name: 'App',
  data() {
    return {}
  },
  components: {
    [Button.name]: Button,
    Feedback,
  },
  mounted() {
    this.handleAdd()
  },
  methods: {
    handleAdd() {
      if (feeback) {
        feeback.remove()
      }
      feeback = create(Feedback)
    },
    handleInit() {
      let node = document.getElementById('m-feedback')
      if (node) {
        node.parentNode.removeChild(node)
      }
      let newElement = document.createElement('div')
      newElement.id = 'm-feedback'
      newElement.innerHTML = 'Hello World'
      document.body.appendChild(newElement)
    },
  },
}
</script>

我开发的chatgpt项目:

https://chat.xutongbao.top/

相关推荐

  1. vue.js中如何使用动态

    2024-02-08 05:56:04       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-08 05:56:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-08 05:56:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-08 05:56:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-08 05:56:04       20 阅读

热门阅读

  1. MySQL常用命令

    2024-02-08 05:56:04       27 阅读
  2. Mongodb聚合:$planCacheStats

    2024-02-08 05:56:04       26 阅读
  3. 人工智能之估计量评估标准及区间估计

    2024-02-08 05:56:04       31 阅读
  4. Flink大状态和Checkpoint调优

    2024-02-08 05:56:04       28 阅读
  5. Elasticsearch 安装和配置脚本文档

    2024-02-08 05:56:04       29 阅读
  6. Elasticsearch基于分区的索引策略

    2024-02-08 05:56:04       30 阅读
  7. Python在无人战争机器人

    2024-02-08 05:56:04       28 阅读
  8. homework day6

    2024-02-08 05:56:04       32 阅读
  9. 假期作业 2月6号

    2024-02-08 05:56:04       28 阅读
  10. PyTorch中torchvision库的详细介绍

    2024-02-08 05:56:04       30 阅读