Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createApp({}).mount('#app') clears #app's child elements in vue3

Tags:

vue.js

vuejs3

So I'm trying to add Vue3 to an existing asp.net core project. What I'd like to happen is for my razor app to render as normal, then use custom vue components to give my frontend a more reactive feel. However, when I mount an empty vue app to my wrapper div (parent of all other body content), it seems to be deleting all innerHTML of that wrapper div, completely removing all server rendered body content.

In my _Layout.cshtml file, I'm wrapping all content in a div with id 'app'.

<body>
  <div id='app'>
    @RenderBody()
  </div>

  <script src="~/js/vue-app/dist/js/chunk-vendors.76316534.js"></script>
  <script src="~/js/vue-app/dist/js/app.bf4c5ba9.js"></script>
</body>

in main.js

import { createApp } from 'vue'

const vueApp = createApp({}).mount('#app');

// component definitions below

With the app set up like this, when I run my .net project I see a blank white browser window instead of the razor compiled html that I expect. In Vue2, it was possible to do this:

const vueApp = new Vue({
  el: '#app',
  data: {
    ....
  },
  methods: {
   ....
  }//, etc
});

Which would result in the app being rendered as normalthe vue app bound to #app, making vue available to the child content (model binding, vue click handling, etc).

I've tried playing around with the isHydrate optional parameter on mount(), but it causes no change in the result.

Am I missing something here? How do you slowly migrate an existing project to use vue3 if you can't mount the app without clearing content? Any guidance is much appreciated.

Thank you

Notes:

  • vue-next runtime-dom source If this method is the mount method getting called, I'm not sure why container.innerHTML would not be getting set in the component. {} is not a function, and render/template is not defined for it.
  • vue-next runtime-core apiCreateApp source If this is the method getting called....I have no idea.
like image 691
Nick Avatar asked Sep 28 '20 19:09

Nick


People also ask

What does Vue createApp do?

createApp() # Creates an application instance. The first argument is the root component. The second optional argument is the props to be passed to the root component.

What is defineComponent?

The concepts behind defineComponent bring us back to the roots of JavaScript—the pure functions and TypeScript function overloading. To make it easy for users, all the logic is hidden behind one name: defineComponent . We always pass any object and return the correctly typed object.

What is Mount in VUE JS?

May 11, 2020. The mounted() hook is the most commonly used lifecycle hook in Vue. Vue calls the mounted() hook when your component is added to the DOM. It is most often used to send an HTTP request to fetch data that the component will then render.


Video Answer


1 Answers

Update

Vue 3, without template renderer, will not be able to handle the templates after it has been compiled. To fix that, you can import vue/dist/vue.esm-browser (and vue.runtime.esm-browser.prod for prod), instead of the default vue. This will allow run-time component rendering.

like image 150
Daniel Avatar answered Nov 02 '22 11:11

Daniel