[Vue警告]:找不到元素 [英] [Vue warn]: Cannot find element

查看:552
本文介绍了[Vue警告]:找不到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Vuejs 。这是我的标记:

I'm using Vuejs. This is my markup:

<body>
  <div id="main">
    <div id="mainActivity" v-component="{{currentActivity}}" class="activity"></div>
  </div>
</body>

这是我的代码:

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

当我加载页面时,我收到此警告:

When I load the page I get this warning:

[Vue warn]: Cannot find element: #main

我做错了什么?

推荐答案

我认为问题是你的脚本是在加载目标dom元素之前执行的在dom中...一个原因可能是您已将脚本放在页面的头部或放在div元素 #main 之前的脚本标记中。因此,当脚本执行时,它将无法找到目标元素,从而导致错误。

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
})

这篇关于[Vue警告]:找不到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆