Imagin a simple custom component like this: MyComponent.vue
<template>
<p>
<div>Test {{ message }}</div>
<div ref="container"></div>
</p>
</template>
<script>
export default {
data() {
return {
message: ""
};
},
mounted() {
console.log(this.$store);
},
methods: {
show(message) {
this.message = message;
console.log(this.message);
console.log(this.$refs.container);
},
},
};
</script>
I could use it like this in an other template:
<template>
<MyComponent />
</template>
<script>
import MyComponent from "./MyComponent";
export default {
components: {
MyComponent,
},
};
</script>
This is working fine. Now I would like to add this component programmatically.
import { h, render } from "vue";
const vnode = h(MyComponent);
let el = document.createElement("div");
document.body.appendChild(el);
render(vnode, el);
vnode.type.methods.show(message);
This works, but I cannot access globally added instance like veux store, router, registered components and directives. It looks like it renders on a own Vue instance. How add the global vue instance to this? Also the message gets not updated via the show() method. Also this.$refs.container is undefined.
Has anyone an idea to do this the right way? I was able to do this using Vue2, but that is not working in Vue3
methods.