我有一个如下所示的vue组件:
<template>
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="true" data-delay="5000">
<!-- HTML Clipped -->
<div class="toast-body">{{message}}</div>
</div>
</template>
<script>
export default {
props: ['title', 'message']
}
</script>
然后我有一个eventListener,它监听通过postMessage发送的消息。确实可以,但是我认为安装不是执行我想要的正确方法。
window.addEventListener('message', e => {
let toastComp = Vue.extend(Toast)
let toast = new toastComp({
propsData: {
message: 'hello'
}
}).$mount('.toast-container')
})
我要寻找的是vue将组件附加到.toast-container
元素上,而不是替换该元素。该怎么办?
答案 0 :(得分:1)
如何在.toast-container
内创建和附加元素,然后将组件安装到该新元素上?
window.addEventListener('message', e => {
const toastContainer = document.querySelector('.toast-container')
const mountNode = document.createElement('div')
mountNode.id = 'mount-node'
toastContainer.appendChild(mountNode)
let toastComp = Vue.extend(Toast)
let toast = new toastComp({
propsData: {
message: 'hello'
}
}).$mount('#mount-node')
})