Append Component programatically show error "Could not find defaults instance" #16026
-
In Vue 2, I append component by Vue.extend({}) and it work fine,
Appended Component <template>
<v-snackbar v-model="innerValue" :timeout="innerTimeout" :color="innerColor">
{{ text }}
</v-snackbar>
</template>
<script>
import { defineComponent } from "vue";
import { useVModel, useReadOnly } from "@/composables/use-vmodel";
export default defineComponent({
name: "Notify",
props: {
modelValue: Boolean,
text: {
type: String,
default: () => "",
},
color: {
type: String,
default: () => "info",
},
timeout: {
type: Number,
default: () => 3000,
},
},
emits: ["update:modelValue"],
setup(props) {
const innerValue = useVModel(props, "modelValue");
const innerTimeout = useReadOnly(props, "timeout");
const innerColor = useReadOnly(props, "color");
return {
innerValue,
innerTimeout,
innerColor,
};
},
});
</script> Vue 3 Append Way1: Use [mount-vue-component] Plugin const instance = getCurrentInstance();
const comp = {
props: ['title'],
setup: (props) => () => h(Notify),
unmounted() { console.log("Bye") },
mounted() { console.log("Hi") }
};
comp.appContext = instance?.appContext ?? null
const { vNode, destroy, el } = mount(comp, {
props: {
...defaultProps,
modelValue: true,
color: "error",
text,
}
}); 2 : Use const vnode= createVNode(Notify, {
...defaultProps,
modelValue: true,
color: "error",
text,
});
const tempDiv = document.createElement('div');
render(vnode, tempDiv);
document.body.appendChild(tempDiv) Vue 2 Append Way1: Vue.extend const ComponentClass = Vue.extend(Notify);
let d = new ComponentClass({
propsData: {
value: true,
color: "error",
text,
...defaultProps
}
});
d.$mount();
document.getElementById("app").appendChild(d.$el); VesionVuetify Version: 3.0.0 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Vue 2 needed |
Beta Was this translation helpful? Give feedback.
-
Oh! I realized. update code here:
import App from '@/main'
import Notify from "@/components/Common/Notify.vue"
import { mount } from 'mount-vue-component'
const defaultProps = {
timeout: 3000
}
export const useNotify = () => ({
error: (text) => {
const { vNode, destroy, el } = mount(Notify, {
props: {
...defaultProps,
// modelValue: true,
color: "error",
text,
},
app: App
});
}
}) |
Beta Was this translation helpful? Give feedback.
Vue 2 needed
new ComponentClass({ vuetify })
, Vue 3 needsapp.use(vuetify)