Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Vue.js needs to be add in the body tag

I added my vue.js in the header but it has an error that says element root can't be found, because I have a div with an id of root.

But when I add it on the body everything works fine.

This is my js code.

new Vue({
     el: '#root'
});

And this is my html.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
	
    <script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
</head>
<body>

<div id="root">

    <task-list>


    </task-list>


</div>


</body>
</html>
like image 478
vlad awtsu Avatar asked Oct 27 '25 16:10

vlad awtsu


2 Answers

you need to import your vue js library at the bottom part of your body because your #app HTML container needs to be loaded first before it can be analysed by vue js.

<body>

<div id="app">
</div>
<script src="vue.js"></script>

</body>

like image 171
Ian Calaunan Avatar answered Oct 29 '25 06:10

Ian Calaunan


Hi I didn't see any included js(except for vue package) in your html. But I think the solution is to place your script in footer.

Also if that doesnt work place it inside window.onload

window.onload = function() {
    new Vue({
        el: '#root'
    });
};
like image 41
Kzy Avatar answered Oct 29 '25 06:10

Kzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!