怎么在vue中创建组件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
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
}
import Vue from 'vue'
export function create(Component, props) {
// 借鸡生蛋new Vue({render() {}}),在render中把Component作为根组件
const vm = new Vue({
// h是createElement函数,它可以返回虚拟dom
render(h) {
console.log(h(Component,{ props }));
// 将Component作为根组件渲染出来
// h(标签名称或组件配置对象,传递属性、事件等,孩子元素)
return h(Component, { props })
}
}).$mount() // 挂载是为了把虚拟dom变成真实dom
// 不挂载就没有真实dom
// 手动追加至body
// 挂载之后$el可以访问到真实dom
document.body.appendChild(vm.$el)
console.log(vm.$children);
// 实例
const comp = vm.$children[0]
// 淘汰机制
comp.remove = () => {
// 删除dom
document.body.removeChild(vm.$el)
// 销毁组件
vm.$destroy()
}
// 返回Component组件实例
return comp
}
A组件(要动态创建的组件)
<template>
<div class="a">
<h3>{{ title }}</h3>
<p>{{ data }}</p>
</div>
</template>
<script>
export default {
props: {
title: {
type: String,
default: "hello world!"
},
message: {
type: String,
default: "o(∩_∩)o 哈哈"
},
duration: {
type: Number,
default: 1000
}
},
data() {
return {
data: "我是a组件",
};
},
created() {
let num = 1
const timer = setInterval(() => {
this.data = num++
}, this.duration)
this.$once("hook: beforeDestroy", () => clearInterval(timer))
}
};
</script>
<style>
.a {
position: fixed;
width: 100%;
top: 16px;
left: 0;
text-align: center;
pointer-events: none;
background-color: #fff;
border: grey 3px solid;
box-sizing: border-box;
}
</style>
B组件(操作动态创建组件的地方)
<template>
<div class="b">
<button @click="createA">创建</button>
</div>
</template>
<script>
import A from "@/components/A.vue"
import { create } from "@/utils/create.js"
export default {
components: {
A,
},
methods: {
createA() {
// 创建A组件,并挂载到body上
create(A, { title: "vue", message: "" })
}
},
};
</script>
看完上述内容,你们掌握怎么在vue中创建组件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。