javascript - Vue3: how to avoid Vitejs to erasereplace all DOM content when mounting the app? - Stack Overflow

I e from Vue 2 and I was used to have my wordpress standard html pages and use some Vue ponents inside

I e from Vue 2 and I was used to have my wordpress standard html pages and use some Vue ponents inside them. Just passed the container to the main app el property, and then I could use ponents on the container, where I wanted to, without having all the default container's content be erased or replaced. Basically, passing a dom element to the el property, maked that element a Vue app, with all its contents (even without any Vue ponents).

With Vue 3 and createApp, it seems there's no way of doing it, since as soon as I mount the app to a container (say #app), then all the container's contents are erased or replaced with ponent's template (if a ponent is passed). Even the following, which doesn't use any ponent, is going to clear all the #app original contents:

import { createApp } from 'vue'

createApp({

}).mount('#app');

How to avoid? How to just use some spare ponents on a free html page? And how about SFC used the same way?

I e from Vue 2 and I was used to have my wordpress standard html pages and use some Vue ponents inside them. Just passed the container to the main app el property, and then I could use ponents on the container, where I wanted to, without having all the default container's content be erased or replaced. Basically, passing a dom element to the el property, maked that element a Vue app, with all its contents (even without any Vue ponents).

With Vue 3 and createApp, it seems there's no way of doing it, since as soon as I mount the app to a container (say #app), then all the container's contents are erased or replaced with ponent's template (if a ponent is passed). Even the following, which doesn't use any ponent, is going to clear all the #app original contents:

import { createApp } from 'vue'

createApp({

}).mount('#app');

How to avoid? How to just use some spare ponents on a free html page? And how about SFC used the same way?

Share Improve this question edited Sep 8, 2021 at 13:14 Luca Reghellin asked Sep 7, 2021 at 13:13 Luca ReghellinLuca Reghellin 8,13515 gold badges84 silver badges132 bronze badges 2
  • You need to be more specific than that, Vue 3 still supports in DOM templates – Michal Levý Commented Sep 7, 2021 at 13:41
  • Thank you, I edited my question. Basically, what is changed is that with Vue2's el property, you could pass an existing piece of html page and get it as a Vue app. This is not the case anymore. It seems there's no way to retain existing markup. – Luca Reghellin Commented Sep 7, 2021 at 14:05
Add a ment  | 

1 Answer 1

Reset to default 7

Technically Vue always replaces the content of the element (Vue 2 was replacing the element itself, Vue 3 replaces it's inner content - migration guide)

IF the app/ponent being mounted has no template option or render function, then (and only then) Vue takes the original content of the DOM element (before it mounts) and tries to pile it into render function (that is why you need Compiler + runtime build when using in-DOM templates)

It also means that if your page consists mostly of static HTML (from WordPress) and only "small sprinkles" of Vue, this solution is very ineffective and reason why petit-vue exists (I remend reading Comparison with standard Vue

Anyway. I'm not able to reproduce your problem...

const app = Vue.createApp({
}).mount("#app")
<script src="https://unpkg./[email protected]/dist/vue.global.js"></script>
<div id='app'>
  <div>
    <h2>Hello from HTML!</h2>
  </div>
</div>

So what can be wrong?

  1. Check browser Dev tools console for any error message - if the pilation step (of the template retrieved from DOM) fails, Vue would indeed render nothing and it would feel like "Vue erases everything after mount"
  2. Beware of DOM Template Parsing Caveats. Browsers tend to "fix" invalid HTML before Vue reads it from the DOM - can cause strange errors
  3. Be sure to use a Vue distribution which includes template piler. Failing to do so would also lead to an error and all content erased

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744767205a4592533.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信