import adCom from "../components/ad/adCom.vue"
class AD4SigleInsClass { //为了单例创建的该类
constructor(Vue) {
this.adComClass = Vue.extend(adCom)
this.addComIns = undefined
}
//getInstance
init() {
if (this.addComIns) {
return this.addComIns
} else {
this.addComIns = new this.adComClass()
return this.addComIns
}
}
}
export default {
install: function (Vue) {
const ad4SingleIns = new AD4SigleInsClass(Vue)
let adIns = ad4SingleIns.init()
adIns.$mount()
document.body.appendChild(adIns.$el)
Vue.prototype.$changeAD =(contentStr) =>{
adIns.content = contentStr
}
}
}
https://cn.vuejs.org/v2/api/#vm-mount
手动挂载vue实例,达到一个应用有多个vue实例,注意挂载的dom节点互相独立就行。
document.body.appendChild(adIns.$el)是全新的dom节点。
本文介绍如何使用Vue.js创建一个单例广告组件,通过手动挂载实例实现应用内多个独立Vue实例共用同一广告组件,确保广告内容的统一性和高效性。
1798

被折叠的 条评论
为什么被折叠?



