Mounting components individually without a root like #app

Neph

I have a page rendered from the back-end and I was hoping to be able to load Vue3 and mount the app and components without an ID. I was informed that this isn't difficult, but clearly my Vue kung fu is weak. I thought this would be a fairly common flow, or maybe I'm looking in the wrong places. I did stumble on petite-vue, but the use at your own risk warning is sketchy for a production build.

This is where I'm at so far, any help is greatly appreciated:

Pre-rendeded markup:

<body>
 <hello-world :msg="prop passed from BE"></hello-world>
</body>

vue.config.js:

module.exports = {
  filenameHashing: false,
  runtimeCompiler: true,
  transpileDependencies: true,
  configureWebpack: {
    devtool: "eval-source-map",
    entry: {
      main: "../exec.js",
    },
  },
  productionSourceMap: true
};

Exec.js (entry point):

import HelloWorld  from "./src/components/HelloWorld.vue"
import { createApp } from "vue";
import compile from "vue"

const app = createApp({})
app.mount()
app.component("HelloWorld", HelloWorld)

HelloWorld.vue:

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  props: {
    msg: {
      type: String,
      default: null,
    },
  },
  name: "HelloWorld",
};
</script>
</script>
    <style lang="scss">
    .root {
      text-align: center;
      color: red;
    }
    </style>

I did notice something while writing this question. If I load https://unpkg.com/[email protected]/dist/vue.global.js and then mount #app it's non-destructive and I weirdly can even see the styles from helloworld.vue being applied, but no template rendering.

app.vue:

<script>
import HelloWorld from "./components/HelloWorld.vue";
export default {
  name: "App",
  components: {
    HelloWorld
  },
};
</script>

exec.js:

import { createApp } from "vue";
import App from "./src/App.vue"
const app = createApp(App)
app.mount("app")

HTML:

<body id="app">
 <hello-world :msg="prop passed from BE"></hello-world>
</body>
Alexander Nenashev

You can create and mount a component to any DOM element you want:

import { createVNode, render } from 'vue';
export function mountComponent(app, elem, component, props) {

    let vNode = createVNode(component, props);
    vNode.appContext = app._context;
    render(vNode, elem);
    return vNode.component;
}
  • app - vue3 app instance
  • elem - DOM element to mount to
  • your component
  • props to pass to your newly created component

To match your prerendered HTML:

<body>
 <hello-world data-msg="prop passed from BE"></hello-world>
</body>
import {app} from './exec';
import HelloWorld from "./components/HelloWorld.vue";
import {mountComponent} from './utils';

document.querySelectorAll('hello-word').forEach(elem => {

   mountComponent(app, elem, HelloWorld, elem.dataset);

   // we try to remove <hello-world>, not sure about to work ok

   // move the component's DOM before the <hello-word>
   elem.children.forEach(child => elem.parent.insertBefore(child, elem));

   // remove <hello-world> not to clutter the page
   elem.remove();

});

You could also try to use Web Components: https://vuejs.org/guide/extras/web-components.html

Overall it's an interesting topic, tomorrow I'll try to bring some live examples on my github.

Moreover it's more interesting with Vite's globbing your components and putting them into the DOM automatically... Will try to make an example tomorrow. Time to sleep.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mounting USB mass storage devices without root access

Mounting a Components Library in Vue 3

How to access database on Android app without root

Resolving symlinks in Python without failing with missing components (like "readlink -m")

Plug in hard drive without mounting

mounting a SD card without a partition

Mounting without needing sudo *afterwards*

NX - UI Library with Storybook and individually exported components

What is the correct way to import Vuetify components individually?

Render and test UI components Individually in angular 6

Multiple KnockoutJs components on the same page running individually

How to individually style Angular material components?

Mounting external scripts that transform the DOM in React Components?

Moving root partition: fstab mounting old partition

Are there any security risks when mounting as root?

cifs mounting all files as root owner

ADMA error after mounting root partition

Gentoo mounting root as read only, why?

Effects of mounting / (filesystem root) with nodev option

Mounting as <user>, a loop still assigns root ownership

Docker -- mounting a volume not behaving like regular mount

Shiny App: How to collect all text inputs into a data frame without listing them individually (how to index reactive values?)

Use react native components in Android app without creating a new Activity

How do I get the APK of an installed app without root access?

Removing App from 'Overview' force stops it [with/without root]

Is there a way to use styled-components in a React app, without renaming all existing components?

Solve quadratic equations without individually specifying cofficients

How to create Rails app without comments in files like Gemfile and etc?

How to make text style like groupon app without using webview?