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>& ...
随机推荐
- 记录 shell学习过程(3) if 的格式
] #-e 为检测目录或文件是否存在 !为取反 then mkdir -v /tmp/ echo 'ok' fi if else if [ $USER == 'root' ] then echo 'h ...
- 打表法fffff
打表法: 打表前先运行一下 看一看速度 优化: O3 #pragma GCC optimize(3,"Ofast","inline") O2 #pragma ...
- Quality and CCPC
English foundation: the fractional part 小数部分 disclaimer 免责声明 fictitious 虚构的,编造的;假定的,虚设的;小说式的;假装的 No ...
- The number of set(位运算+状态dp)一道十分美妙的题目哦!!!!!!
Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use the ...
- 安全相关的Linux知识
本文用于记录在安全中的Linux常用命令,基础命令可以移步去菜鸟教程(https://www.runoob.com/linux/linux-tutorial.html)学习 Linux重要的4个热键 ...
- ISCC2018_leftleftrightright-Writeup
leftleftrightright(150) 这个题学到了不少东西,值得认真写一下 下载好文件后发现是upx的壳,upx -d直接脱掉后运行,发现是经典的check输入的题目(作为一个linuxer ...
- 安卓模拟器genymotion 安装使用
下载:https://www.genymotion.com/download/ 安装前先注册: https://www.runoob.com/w3cnote/android-tutorial-geny ...
- web-css-文本
一.文本的水平对齐方式 使用text-align来设置文本的对齐方式:text-align的取值:left(向左对齐)/center(水平居中对齐)/right(向右对齐)/justify(两端对齐) ...
- 2.10 webdriver中 js 使用
来源: 使用Webdriver执行JS小结 http://lijingshou.iteye.com/blog/2018929 selenium常用的js总结 http://www.cnblogs. ...
- 阿里配置docker镜像专属地址
阿里配置docker镜像专属地址 待办 https://www.jianshu.com/p/6b416dff0691