"platform" is undefined when using quasar components and rendering component manually #16727
-
Hello! I'm currently trying to setup a simple confirmation dialog composable that uses quasar components and the HTML The composable is this: import { DeferredPromise } from "@/modules/core/DeferredPromise";
import ConfirmationDialogVue from "@/modules/core/components/ConfirmationDialog.vue";
import { h, render } from "vue";
export function useConfirmationDialog() {
return function (title?: string, confirmText?: string, cancelText?: string) {
const deferred = new DeferredPromise<boolean>();
const body = document.querySelector("body")!;
const container = body.appendChild(document.createElement("div"));
const renderedDialog = h(ConfirmationDialogVue, {
title,
confirmText,
cancelText,
onConfirmed() {
render(null, container);
container.remove();
deferred.resolve(true);
},
onCancelled() {
render(null, container);
container.remove();
deferred.resolve(false);
}
});
render(renderedDialog, container);
if (renderedDialog.component && renderedDialog.component.exposed) {
renderedDialog.component.exposed.open(title, confirmText, cancelText);
}
return deferred.getPromise();
};
} Which renders this component: <template>
<dialog ref="dialogElement" class="rounded border-0 shadow-lg">
<div>{{ props.title }}</div>
<QBtn />
<button @click="emit('confirmed')">{{ props.confirmText }}</button>
<button @click="emit('cancelled')">{{ props.cancelText }}</button>
</dialog>
</template>
<script setup lang="ts">
import { ref } from "vue";
const props = withDefaults(
defineProps<{
title?: string;
confirmText?: string;
cancelText?: string;
}>(),
{
title: "",
confirmText: "Confirmar",
cancelText: "Cancelar"
}
);
const dialogElement = ref<HTMLDialogElement>();
const emit = defineEmits<{
(e: "confirmed"): void;
(e: "cancelled"): void;
}>();
defineExpose({
open: () => {
dialogElement.value?.showModal();
}
});
</script> The composable is called like this: const confirm = useConfirmationDialog();
if (await confirm("Are you sure you want to continue?")) {
await doSomething();
} The issue is that when the function is called it throws this error:
It refers to the following section inside the button component: if (isActionable.value === true) {
const acc = {
onClick,
onKeydown: onKeydown2,
onMousedown
};
if (proxy.$q.platform.has.touch === true) { // HERE
const suffix = props2.onTouchstart !== void 0 ? "" : "Passive";
acc[`onTouchstart${suffix}`] = onTouchstart;
}
return acc;
}
return {
// needed; especially for disabled <a> tags
onClick: stopAndPrevent
};
}); I'm assuming this has something to do with the way I am rendering the component it does not have access to the vue instance. I couldn't find much information in the vue documentation since this render method seems to be used only internally. Does anyone have any idea on how I could get this to work? The main idea is to render a simple dialog without having to insert any other components in the component that wants to display a confirmation dialog or adding other elements to my main App component. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
I also encountered the same problem. Have you solved this problem? |
Beta Was this translation helpful? Give feedback.
-
Are you using the Quasar CLI? If not, most Quasar components use some internal logic which means you can't simply import Quasar components without a build/bundler configuration (e.g. the Vite plugin). |
Beta Was this translation helpful? Give feedback.
-
For anyone who runs into this issue later, Stefan mentioned the |
Beta Was this translation helpful? Give feedback.
gkovalechyn/quasar-platform-undefined#1