Vue 3 dynamically loaded component hooks are not triggered
P粉063862561
P粉063862561 2024-03-27 20:47:14
0
1
378

I have this (short for question) single file component (vue 3.2.31):

<template lang="pug">
.test Hello world!
</template>
<style lang="sass" scoped>
.test
  font-weight: bold
</style>
<script setup lang="ts">

onMounted(() => {
  console.log('Mounted');
});

</script>

It is bundled via vitejs, exported as (say) NamedExport, and imported on the client side as a base64 encoded string on demand.

const component = await defineAsyncComponent(async () => {

  // A module that exports multiple components.
  const module = await import(base64StringSentFromTheServer);

  // Choose one.
  return module['NamedExport']);

})

Then, the result must be:

<component :is="component" />

It works just fine , except for two things, one of which is that the hook is not called (in this case onMounted) and the other is that neither is the style importer is called.

Is this expected behavior, or am I missing something? Is <script setup> the way to write responsible components?

P粉063862561
P粉063862561

reply all(1)
P粉128563140

It appears that I have two instances of Vue running (one bundled with my package, with rollups, and the other imported in the script itself), and for unknown reasons, both None of the instances are running the call hook.

By removing one of the instances (actually passing vue as external in the rollup build configuration) it now works fine.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template