vue动态生成组件
单个组件引用,引入此文件js。全局使用,注册到vue的main文件Vue.prototype.create = Create
create.js
import Vue from 'vue';
import Toast from './toast'; 组件名
function create(component, props) {
let temp = null;
switch (component) {
case 'toast':
temp = Toast;
break;
}
const vm = new Vue({
// 为什么不使用 template 要使用render 因为现在是webpack里面没有编译器 只能使用render
render: h => h (temp, {props}) // render 生成虚拟dom {props: props}
}).$mount(); // $mount 生成真实dom, 挂载dom 挂载在哪里, 不传参的时候只生成不挂载,需要手动挂载
// 手动获取挂载dom元素
document.body.appendChild(vm.$el);
// 回收组件
const comp = vm.$children[0];
comp.remove = () => {
document.body.removeChild(vm.$el); // 删除元素
comp.$destroy(); // 销毁组件
};
return comp;
}
export default create;
toast组件
<template>
<section class="loading-bg" v-if="show">
<section class="toast">
{{content}}
</section>
</section>
</template> <script>
export default {
name: 'toast',
props: {
second: {
type: Number,
default: 2.5
},
content: {
type: String,
default: ''
}
},
data() {
return {
show: true
}
},
created() {
setTimeout(() => {
this.show = false;
}, this.second * 1000);
}
}
</script> <style scoped>
.loading-bg{
position: fixed;
z-index: 99999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0);
}
.toast{
position: absolute;
top: 0;
left: 50%;
bottom: 0;
margin: auto 0;
padding: 0 6px;
height: 30px;
line-height: 30px;
font-size: 14px;
transform: translateX(-50%);
border-radius: 4px;
white-space: nowrap;
background: rgba(0,0,0,.7);
color: #fff;
}
</style> 调用的地方
this.create('toast', {
content: '你好啊'
})
或者
let aa = Create('toast', {
second: 2,
content: '你好啊'
})
setTimeout(() => {
aa.remove();
}, 1000)
vue动态生成组件的更多相关文章
- vue动态生成表单
vue动态生成表单 一.下拉框 <!DOCTYPE html> <html> <head> <meta charset="utf-8" / ...
- 解决 Vue 动态生成 el-checkbox 点击无法赋值问题
博客地址:https://ainyi.com/68 最近遇到一个问题,在一个页面需要动态渲染页面内的表单,其中包括 checkbox 表单类型,并且使用 Element 组件 UI 时,此时 v-mo ...
- Vue动态创建组件方法
组件写好之后有的时候需要动态创建组件.例如: 编辑文章页面,正文是一个富文本编辑器,富文本编辑器是一个第三方的组件,点击添加章节的时候需要动态的创建一个富文本编辑器这个时候怎么处理呢. 富文本编辑器也 ...
- vue动态子组件的实现方式
让多个组件使用同一个挂载点,并动态切换,这就是动态组件. 通过使用保留的 <component>元素,动态地绑定到它的 is 特性,可以实现动态组件. 方式一:局部注册所需组件 <d ...
- 第八十七篇:Vue动态切换组件的展示和隐藏
好家伙, 1.什么是动态组件? 动态组件指的是动态切换组件的限制与隐藏 2.如何实现动态组件渲染 vue提供了一个内置的<component>组件,专门用来实现动态组件的渲染. 可以将其看 ...
- VUE动态生成table表格(element-ui)(新增/删除)
(直接复制即可测试) 结构(红色部分 data/prop/v-model 数据绑定): <template> <el-table size="small" :da ...
- 使用Vue动态生成form表单
form-create 表单生成器 具有数据收集.校验和提交功能的表单生成器,支持双向数据绑定和事件扩展,组件包含有复选框.单选框.输入框.下拉选择框等表单元素以及省市区三级联动,时间选择,日期选择, ...
- 【Html】Vue动态插入组件
html: <div id="app"> <p>{{ message }}</p> <button @click="add('a ...
- vue动态切换组件
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
随机推荐
- Python实现求多个集合之间的并集
目的:求多个集合之前的并集,例如:现有四个集合C1 = {11, 22, 13, 14}.C2 = {11, 32, 23, 14, 35}.C3 = {11, 22, 38}.C4 = {11, ...
- EF CodeFirst关于Mysql如何自动生成数据库表
相对于sqlserver数据库,mysql的配置过程相对麻烦一些,我们从0讲起. 1.新建一个控制台应用程序 右键点击引用--管理NuGet程序包,搜索Mysql.Data.Entity并安装,安装完 ...
- TD - 系统异常 - 登录不上
问题描述:登录之后,页面停留在登录页面,没有任何错误提示 解决办法:重新安装插件
- LocalDate和LocalTime的用法介绍
原文:LocalDate和LocalTime的用法介绍 在JAVA中,常用的处理日期和时间的类主要有Date,Calendar,而在JDK1.8中,新增了两个处理日期和时间的类,一个是LocalDat ...
- C short类型的内存分析
#include<stdio.h> #include<limits.h> void main(){ //printf("short%d, int%d, long%d ...
- MonoBehaviour单例的另外一种省事的写法
using UnityEngine; public class CommSystem: SingletonGeneric<CommSystem> { public static strin ...
- MSSQL 打开xp_cmdshell
sp_configure reconfigure go sp_configure reconfigure go
- null值与非null只比较大小时,只会返回false
DateTime? time=null; DateTime now=DateTime.Now; null值与非null只比较大小时,只会返回false 无论是大于比较还是小于比较还是等于,都会返回fa ...
- 配置yum仓库:yum install 软件
1.一个重要模板: 进入/etc/yum.repos.d文件夹,新建一个xiaoxu.repo文件,其中xiaoxu可以根据需要来取名. [模板] vim xiaoxu.repo [rhel] ...
- analog filter
理想的filter如下: 但是实际的filter如下: 在实际应用中,我们更多的是用Fo和Q这两个parameter来design analog filter. Low-Pass Filter tra ...