vue 動(dòng)態(tài)創(chuàng)建組件的兩種方法
Vue動(dòng)態(tài)創(chuàng)建組件實(shí)例并掛載到body
方式一
import Vue from 'vue' /** * @param Component 組件實(shí)例的選項(xiàng)對(duì)象 * @param props 組件實(shí)例中的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函數(shù),它可以返回虛擬dom render(h) { console.log(h(Component,{ props })); // 將Component作為根組件渲染出來 // h(標(biāo)簽名稱或組件配置對(duì)象,傳遞屬性、事件等,孩子元素) return h(Component, { props }) } }).$mount() // 掛載是為了把虛擬dom變成真實(shí)dom // 不掛載就沒有真實(shí)dom // 手動(dòng)追加至body // 掛載之后$el可以訪問到真實(shí)dom document.body.appendChild(vm.$el) console.log(vm.$children); // 實(shí)例 const comp = vm.$children[0] // 淘汰機(jī)制 comp.remove = () => { // 刪除dom document.body.removeChild(vm.$el) // 銷毀組件 vm.$destroy() } // 返回Component組件實(shí)例 return comp }
使用
A組件(要?jiǎng)討B(tài)創(chuàng)建的組件)
<template> <div class="a"> <h2>{{ title }}</h2> <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組件(操作動(dòng)態(tài)創(chuàng)建組件的地方)
<template> <div class="b"> <button @click="createA">創(chuàng)建</button> </div> </template> <script> import A from "@/components/A.vue" import { create } from "@/utils/create.js" export default { components: { A, }, methods: { createA() { // 創(chuàng)建A組件,并掛載到body上 create(A, { title: "vue", message: "么么噠😙" }) } }, }; </script>
以上就是vue 動(dòng)態(tài)創(chuàng)建組件的兩種方法的詳細(xì)內(nèi)容,更多關(guān)于vue 動(dòng)態(tài)創(chuàng)建組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue.js將unix時(shí)間戳轉(zhuǎn)換為自定義時(shí)間格式
這篇文章主要介紹了vue.js將unix時(shí)間戳轉(zhuǎn)換為自定義時(shí)間格式的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01vue+axios?methods方法return取不到值問題及解決
這篇文章主要介紹了vue+axios?methods方法return取不到值問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue實(shí)現(xiàn)導(dǎo)入json解析成動(dòng)態(tài)el-table樹表格
本文主要介紹了vue實(shí)現(xiàn)導(dǎo)入json解析成動(dòng)態(tài)el-table樹表格,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02解決vue2.0動(dòng)態(tài)綁定圖片src屬性值初始化時(shí)報(bào)錯(cuò)的問題
下面小編就為大家分享一篇解決vue2.0動(dòng)態(tài)綁定圖片src屬性值初始化時(shí)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03nginx+vite項(xiàng)目打包以及部署的詳細(xì)過程
我們使用nginx部署Vue項(xiàng)目,實(shí)質(zhì)上就是將Vue項(xiàng)目打包后的內(nèi)容同步到nginx指向的文件夾,下面這篇文章主要給大家介紹了關(guān)于nginx+vite項(xiàng)目打包以及部署的相關(guān)資料,需要的朋友可以參考下2023-01-01