[Vue warn]: Cannot find element

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I'm using Vuejs. This is my markup:

     

This is my code:

var main = new Vue({     el: '#main',     data: {         currentActivity: 'home'     } }) ; 

When I load the page I get this warning:

[Vue warn]: Cannot find element: #main 

What am I doing wrong?

回答1:

I think the problem is your script is executed before the target dom element is loaded in the dom... one reason could be that you have placed your script in the head of the page or in a script tag that is placed before the div element #main. So when the script is executed it won't be able to find the target element thus the error.

One solution is to place your script in the load event handler like

window.onload = function () {     var main = new Vue({         el: '#main',         data: {             currentActivity: 'home'         }     }); } 

Another syntax

window.addEventListener('load', function () {     //your script }) 


回答2:

I get the same error. the solution is to put your script code before the end of body, not in the head section.



回答3:

I've solved the problem by add attribute 'defer' to the 'script' element.



回答4:

The simple thing is to put the script below the document, just before your closing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!