如果元素存在,则实例Vue? [英] Instance Vue if element exists?

查看:20
本文介绍了如果元素存在,则实例Vue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,其中一个页面有一些虚拟机,而另一些则没有.当我们从一个页面切换到另一个页面时显示以下警告:

I'm building an app that a page has some vms, in others not. When we change from one page to another show the following warning:

[Vue 警告]:找不到元素:#element-id

如何仅在页面上有元素的情况下实例化虚拟机并避免错误?

How do I instantiate VMs only if there is the element on the page and avoid the error?

注意:我正在使用 Rails 资产管道,因此,所有 javascript 都连接到一个文件中.

NOTE: I'm using Rails asset pipeline, so, all the javascript is concatenated into a single file.

推荐答案

保持原样没有问题.这只是一个仅在调试模式打开时运行的警告,因此无论如何您都应该在生产中将其关闭.

There are no issues just leaving it as is. This is just a warning that only runs when debug mode is on, so you should have it turned off in production anyway.

如果你想摆脱它,只需在启动Vue之前检查元素是否存在-

If you want to get rid of it, just check if the element exists before launching Vue -

if(document.getElementById("element-id")){
  new Vue({...})
}

Ref: 如何检查元素是否存在于可见的 DOM?

这篇关于如果元素存在,则实例Vue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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